检查屏幕以查看使用PHP

时间:2018-09-19 03:09:32

标签: php linux centos

我有一个php脚本,用于使用进程名称执行到CentOS服务器的命令

screen -dmS $processName ./build.sh

我希望能够检查该进程名称是否已经在屏幕上运行,并且是否正在运行,所以不要执行脚本,而是将结果输出回php,例如

$processName = "myProcess";

//Check to see if the process is running using screen -ls and return yes or no back to the php script
if(!($stream = ssh2_exec($con, "screen -ls"))){
"there is a screen on: 11111.myProcess (Detached)" //Returns 'yes' to PHP
"No Sockets Found" //Returns 'no' to PHP

  if($processReturn == "yes"){
  //Do not start the process
    echo "a process is already running in that name";
  } else {
    //start the process with the name myProcess
    if(!($stream = ssh2_exec($con, "screen -dmS ".$processName." ./build.sh")));
  }

更新:针对可能需要解决此问题的人,自己解决了此问题。

if(!empty($prCheck))
    {
        $proc = $_GET['prCheck'];
        $stream = ssh2_exec($con, "screen -ls | grep ".$proc);
        stream_set_blocking($stream, true);
        $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
        $output = stream_get_contents($stream_out);
        $sep = explode('.', $output);
        $result = explode(" (",$sep[1]);
        $fin = $result[0];
        if($fin == $proc){
            echo $fin;
        }
    }

这仅获得进程名称,没有其他内容(没有id,没有点,没有空格并且没有(分离的)区域)

0 个答案:

没有答案