Question

I am using phpseclib, Net_SSH2.

I need to get the process ID of the server/process that is started by this command.

echo $ssh->exec('java -Xmx256M -Xms32M -jar minecraft_server.jar nogui');

This is so i can store it and be able to kill it via a PHP script at anytime.

Was it helpful?

Solution

You may try:

echo $ssh->exec('java -Xmx256M -Xms32M -jar minecraft_server.jar nogui > /dev/null 2>&1 & echo $!;');

OTHER TIPS

Even this would work if you want to track PID of particular command after its execution.

$cmd = 'java -Xmx256M -Xms32M -jar minecraft_server.jar nogui';
$pid = $ssh->exec('echo `ps aux | grep -F "' . $cmd . '" | grep -v -F "grep" | awk \'{ print $2 }\'`');
echo $pid;  

To Kill same:

$ssh->exec("kill -9 $pid");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top