【问题标题】:How to retrive the ProcessID of a ssh2_exec process如何检索 ssh2_exec 进程的进程 ID
【发布时间】:2013-12-16 05:37:50
【问题描述】:

谁能解释我如何从以 ssh2_exec 开始的进程中检索 ProcessID?我尝试了很多东西,但它只给出以下消息“资源 id #6”

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

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

【问题讨论】:

  • getmypid() 将给出进程 ID

标签: php libssh2 pid


【解决方案1】:

这将帮助您找到进程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.';
}

【讨论】:

  • 嗯,它唯一输出的是来自 Apache2 的进程 ID。不是我开始的过程。
【解决方案2】:

我认为应该是:

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

【讨论】:

  • 它仍然只给出“Resource id #6”消息,而不是我启动的进程中的实际进程 id。
【解决方案3】:

要通过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);

【讨论】:

    猜你喜欢
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多