如何检索ssh2_exec进程的进程ID

时间:2013-12-16 05:37:51

标签: php libssh2 pid

有人能解释我如何从一个以ssh2_exec开始的进程中回收ProcessID吗?我尝试了很多东西,但它只给出了以下消息“资源ID#6”

下面是我正在努力解决的代码但是没有回应ProcessID我只得到“资源ID#6”

$pid = ssh2_exec($connection, 'cd /home/servers/; nohup ./sc_serv' .$config .' & > /dev/null 2>&1 & echo $!');

3 个答案:

答案 0 :(得分:0)

这将帮助您找到进程ID

<?php

//this will return the process id
$pid = getmypid();

//you can check the process id
if(file_exists('/proc/'.$pid))
{
    echo 'The process is still running.';
}

答案 1 :(得分:0)

我认为应该是:

$pid = ssh2_exec($connection, 'cd /home/servers/; nohup ./sc_serv' .$config .' & > /dev/null 2>&1; echo $!');

答案 2 :(得分:0)

要通过ssh2_exec获取远程计算机上启动的进程的进程ID,您可以执行以下操作:

$cmd = "cd /home/servers/; nohup ./sc_serv' .$config .' & > /dev/null 2>&1 & echo $!"
$stdout_stream = ssh2_exec($connection, $cmd);
$dio_stream = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
stream_set_blocking($dio_stream, true);
$pid = stream_get_contents($dio_stream);