简体   繁体   中英

PHP shell_exec doesn't execute

I'm having this huge headache that is making some buttons on my webserver that stops/start/restart my ElasticSearch service when needed. I've looked up a lot on the internet and not found anything like my problem.

Here is the shell that I'm trying to execute:

if(isset($_POST["name"])){

$name = $_POST["name"];

//the script to check the service status of ElasticSearch (which works fine btw)
$status = shell_exec("systemctl status elasticsearch");

//I use a simple regex to get from the above script's output if the status of the service is "running" or not
preg_match("/\bactive\s+\K\S+/", $status, $result);

switch ($name){
    case "start":
        $shell = shell_exec("sh ../../../../scripts/startElasticsearch.sh");
        echo $shell;
        if($result[0] == "(running)"){
            echo "Node started successfully.";
        }
        break;
    case "restart":
        $shell = shell_exec("sh ../../../../scripts/restartElasticsearch.sh");
        echo $shell;
        if($result[0] == "(running)"){
            echo "Node restarted successfully.";
        }
        break;
    case "stop":
        $shell = shell_exec("sh ../../../../scripts/stopElasticsearch.sh");
        echo $shell;
        if($result[0] !== "(running)"){
            echo "Node stopped successfully.";
        }
        break;
}
}

This is the shell script of the 3 .sh files that I wrote:

#!/bin/bash
systemctl start elasticsearch

#!/bin/bash
systemctl restart elasticsearch

#!/bin/bash
systemctl stop elasticsearch

I've already changed all permissions to execute the shell (like chmod 755 and so on...).

Now here is the trippy part: when I was debugging the execution of the shell, I've tried to echo a "Hello World" so I can be sure that my php script was working fine (since systemctl start/restart/stop elasticsearch doesn't output anything) And it worked all fine. I've got the "Hello World" answer from the script on a toast when clicked the button. But nothing else from the 3 scripts above (I was monitoring the service status via Kibana).

Any ideas?

After some more digging, i found this article that answered my question. It was only a simple mistake of using sh on the script that was causing the problem. Thank you all for your time!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM