使用proc_open和exec,PHP启动,停止和获取进程状态的问题几乎可以正常工作

问题描述

我有一个在LINUX和OS X上运行的服务器。在UNIX系统上有一个init.d脚本,但是我无法在OS X上创建一个等效文件我有一种解决方法,可能在这两个系统上都可以工作,尽管我可能还想添加一些东西以在LINUX上使用init.d脚本,但是也可以将LINUX init.d脚本转换为OSX。

我有一个API,可以获取所需的命令(启动,停止,状态),并且可以调用proc_open函数或exec函数来执行该请求。

简而言之,在PHP类中:

private static $statusscript = "ps aux | awk '$11~/Orthanc/ {PID = $2} END {if (PID) print PID; else print \"OFF\"}'";
private static $startscript = "/Users/sscotti/Desktop/orthancAndpluginsOSX.stable/startOrthanc.command /Users/sscotti/Desktop/orthancAndpluginsOSX.stable/configOSX.json --verbose";
private static $stopscript = "ps aux | awk '" . '$11~/Orthanc/ {PID = $2} END {if (PID) print "kill -9 "PID | "bash"; else print "Already OFF"}' . "'";
private static $orthanclog = "/usr/local/Cellar/openresty/1.17.8.2_1/Nginx/html/orthanc.log";


public static function status() {

    $status = self::proc_open(self::$statusscript);
    $stdout = $status['stdout'];
    $exitcode = $status['exitcode'];
    echo '{"status":"' .  $exitcode .  '","PID":"'  . $stdout .  '"}';

}

public static function start() {

    $command = self::$startscript . " > " . self::$orthanclog . " &";
    exec($command,$output,$exitcode);
    DatabaseFactory::logVariable($exitcode);
    echo '{"status":"' .  $exitcode .  '","PID":  "'  . json_encode($output) .  '"}';

    
}

public static function stop() {

    $status =  OrthancModel::proc_open(self::$stopscript);
    $stdout = $status['stdout'];
    $exitcode = $status['exitcode'];
    if ($stdout == "") $stdout = "Stopped";
    echo '{"status":"' .  $exitcode .  '","Message":"'  . $stdout .  '"}';
}

public static function proc_open($command) {

    $proc = proc_open($command,[
    0 => ['pipe','r'],1 => ['pipe','w'],2 => ['pipe','/usr/local/Cellar/openresty/1.17.8.2_1/Nginx/html/process_error.log',"a"]],$pipes);
    fwrite($pipes[0],'');
    fclose($pipes[0]);
    $stdout = strtok(stream_get_contents($pipes[1]),"\n");  // 1st line of output.
    fclose($pipes[1]);
    $exitcode = proc_close($proc);
    if ($exitcode == -1) $exitcode = "ERROR";
    else $exitcode = "SUCCESS";
    DatabaseFactory::logVariable("stdout:  " .$stdout . "," . $exitcode);
    return array("exitcode" => $exitcode,"stdout" => $stdout);
}

我正在使用proc_open,因为我想从响应中获取一些信息。从那里的终端调用时,传递给该命令的$ command实际上会终止(即停止和状态)。

我正在使用exec进行启动,因为它需要在后台运行,因为它不会终止。我对这两个都不是很熟悉,但是我有兴趣将后台进程的输出保存到文件中。该部分似乎不起作用。

状态返回,其中MESSAGE是PID或OFF:

stdClass Object
(
    [status] => SUCCESS
    [MESSAGE] => 39464
)

stdClass Object
(
    [status] => SUCCESS
    [PID] => OFF
)

停止返回:

stdClass Object
(
    [status] => SUCCESS
    [Message] => Already OFF
)

stdClass Object
(
    [status] => SUCCESS
    [Message] => Stopped
)

开始返回:

stdClass Object. (ALREADY RUNNING)
(
    [status] => 0
    [PID] => []
)

stdClass Object. (Started Up successfully)
(
    [status] => 0
    [PID] => []
)

因此,一切基本上都可以正常工作,但是我需要更多有关Start功能的反馈。我尝试使用proc_open,但是存在一个问题,因为我不知道如何在后台以这种方式运行该进程,并且服务器将挂起。 exec函数有效,但是我没有收到有关PID,成功等的任何反馈,并且无论如何它都不会记录到文件中。

任何有关启动脚本的帮助,或者通常只提供帮助。对于UNIX,我大概可以使用类似的方法运行init.d脚本。对此的建议也很感激(即使用什么,因为我想从中捕获终端输出,因为该脚本在从命令行执行时终止,并且通常有1行输出

理想情况下,如果我可以将UNIX init.d转换为OSX启动守护程序或服务脚本,则可能会更容易,但这实际上在涉及的功能范围内有效(即,我可以获取状态,启动和停止)使用API​​的服务器。)

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)