How to retrive the ProcessID of a ssh2_exec process - php

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

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

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

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

Related

Task not killing with exec() and posix_kill() in Ubuntu Server?

I'm setting a dashbord for manage a game server but I can not kill process with PID. I tried exec("kill $PID") exec("kill".$PID) posix_kill($PID, 15) but it did not work.
A posix_kill command give a "Operation not permitted" error yet the user apache is www-data and the user for the minecraft script is www-data also.
<?php
include('pidminecraft.php') ;
$output = shell_exec("bash /srv/scripts/pidphp.sh $PID ");
if ( $output == "run" )
{ exec('kill'. $PID); shell_exec("bash /srv/minecraft/minecraft_survie/start.sh >> /srv/web/minecraft/minecraft.txt 2>&1"); }
else
{ exec("bash /srv/minecraft/minecraft_survie/start.sh >> /srv/web/minecraft/minecraft.txt 2>&1"); }
?>

PHP: Redirect output of a bash command into default logger

I have a class executing commands in my console. This class is defined with a LoggerInterface defined from #logger.handler.
I want to execute commands in my bash but:
I dont want to display result in default stream
I want to put result in the default log file (defined by $logger), e.g. app/logs/application.log
The main method of this class looks like:
$command = $command . " > 2>&1";
$returnVal = null;
$output = [];
exec($command, $output, $returnVal);
$this->logger->debug($output);
I added > 2>&1 to display nothing in the current stream, and because I don't know how to get app/log/application.log from the $output...
The issue is that I have nothing in my app/config/application.log!
Do you know what I've missed ?
You are not capturing anything in the output because of > 2>&1
//$command = $command . " > 2>&1";
$returnVal = null;
$output = [];
exec($command, $output, $returnVal);
for ($i=0;$i<count($output);$i++)
$this->logger->debug($output[$i]);
if the debugger creates several timestamps for each one and it is not the desired output then use this:
$returnVal = null;
$output = [];
exec($command, $output, $returnVal);
$result=""
for ($i=0;$i<count($output);$i++)
$result.=$output[i]."\n";
$this->logger->debug($result);

While monitor won't stop running (php)

I'm using a while loop to output a "." whilst a server side script is running using exec. This works great apart form the fact that it never stops running, i.e. "........" would continue building to infinity!
Does anyone know a way to make this client side update once it's finished running:
$dbupdate = ($siteurl."/data_update.php");
$runupdate = exec("nohup curl ".$dbupdate." > /dev/null 2>&1 & echo $!");
while(exec("ps $runupdate"))
{
echo(" . ");
ob_flush(); flush();
}
This is because while() will keep executing until the condition is false/null/0, and exec() returns void.
DOCS: http://php.net/manual/en/function.exec.php
Something like this might help?
$runupdate = exec("nohup curl ".$dbupdate." > /dev/null 2>&1 & echo $!");
// Spitballing here...
$pid = true;
// Execute only while $op is strictly true, and not the PID that `echo $!` returns
while( $pid === true )
{
exec("ps $runupdate", $pid);
echo(" . ");
ob_flush(); flush();
}
Check out the docs here: http://php.net/manual/en/function.exec.php#88704

sudo_exec returns nothing

I try to ping www.google.de with shell_exec and store the result into a variable but i get no result back from shell_exec.
<?php
$ping = 'sudo ping -c 4 ';
$url = 'www.google.de';
$command = $ping . $url;
$ping_result = shell_exec($command);
$datei = fopen("/var/www/myProject/result_ping","w") or die ("Could not open file!");
sleep(10);
if ($datei == false)
{
$ping_result = "Cannot open file!";
}
else
{
fwrite ($datei , $ping_result);
fclose ($datei);
}
echo $command; //Output: sudo ping -c 4 www.google.de
echo $ping_result; //Output: nothing
?>
The file result_ping has all rights (chmod 777).
Maybe the webserver is not allowed to execute ping?
Add 2>&1 to your command to ensure you're not getting an error message that shell_exec would filter off:
$command = $ping . $url . ' 2>&1';
shell_exec will return NULL in case of error. With that modification you redirect any error message to normal output, thus forcing shell_exec show every message you would normally get on a console session.

Execute Python with PHP

I try to execute this PHP command :
$result = exec("sudo python /home/pi/test.py",$output, $ret);
var_dump($result);
echo "<br>";
var_dump($output);
echo "<br>";
var_dump($ret);
This command works perfectly on a Linux Terminal but with the PHP it doesn't work.
Here the result on the PHP page :
string(0)
array(0) { }
int(9)
I verified the process with ps -ef, nothing appears.
It might be help someone with the same problem.
I managed later to execute a Python script with this PHP code :
$command = escapeshellcmd('sudo /home/pi/test.py');
$output = shell_exec($command);
echo $output;
For the moment, I never succeed to execute this in background. I try those solutions :
$command = escapeshellcmd("sudo /home/pi/test.py >/dev/null &");
$command = escapeshellcmd("sudo /home/pi/test.py &");
My PHP page wait also the end of the process, but i don't want to.

Categories