簡體   English   中英

PHP shell_exec不執行

[英]PHP shell_exec doesn't execute

我很頭疼,正在使我的網絡服務器上的某些按鈕在需要時停止/啟動/重新啟動我的ElasticSearch服務。 我在互聯網上查找了很多東西,卻沒有發現類似我的問題的東西。

這是我要執行的外殼程序:

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;
}
}

這是我編寫的3個.sh文件的shell腳本:

#!/bin/bash
systemctl start elasticsearch

#!/bin/bash
systemctl restart elasticsearch

#!/bin/bash
systemctl stop elasticsearch

我已經更改了執行Shell的所有權限(例如chmod 755等)。

現在這是一個令人迷惑的部分:調試外殼程序的執行過程中,我嘗試echo “ Hello World”,因此可以確保我的php腳本運行正常(因為systemctl start/restart/stop elasticsearch沒有輸出任何東西),並且一切正常。 單擊按鈕時,我從烤面包片上得到了“ Hello World”答案。 但是上面3個腳本中沒有其他內容(我正在通過Kibana監視服務狀態)。

有任何想法嗎?

經過更多的挖掘,我發現這篇文章回答了我的問題。 導致問題的原因只是在腳本上使用sh只是一個簡單的錯誤。 謝謝大家的時間!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM