dora12345678 2018-06-28 22:55
浏览 114

PHP shell_exec不会执行

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?

  • 写回答

1条回答 默认 最新

  • dongyi1111 2018-07-05 14:37
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败