douluan1533 2018-09-19 03:09
浏览 35

使用PHP检查运行过程的屏幕

I have a php script that executes commands to my CentOS server with process names

screen -dmS $processName ./build.sh

I want to be able to check if that process name is already running in screen and if it is running, do not execute the script but instead output a result back to php, for example

$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")));
  }

UPDATE: Solved the issue myself, for those who may need it.

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

this gets just the process name, nothing else (no id, no dots, no spaces and no (Detached) areas)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度