How to retrive the ProcessID of a ssh2_exec process

404 Views Asked by At

Could anyone explain me how to retrieve the ProcessID from a Process which is started with ssh2_exec? I've tried many things, but its only giving the following message "Resource id #6"

Below is the code where I am struggling with But instead of echo'ing the ProcessID I only get "Resource id #6"

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

There are 3 best solutions below

1
On

This will help you to find the process 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
On

I think it should be:

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

To get the process id of the process started on the remote machine by ssh2_exec, you can do:

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