[英]PHP: Get the LINUX PID of a command
我在我的 linux 服务器(Ubuntu)中运行一个命令。 例如:
screen -A -h 1500 -m -dmS test_command_one /home/SKY-users/SKY-001/./script
有没有办法获得这个后台进程的PID,屏幕名称是: test_command_one
?
ps辅助| grep test_command_one:
root 6573 8.1 2.4 271688 123804 pts/4 Ss+ Oct19 3:04 /home/SKY-users/SKY-001/./ ...
我想取回这个PID: 6573
PHP:(简单)
<?php
$output = shell_exec('sudo ps aux | grep test_command_one');
$array = explode("\n", $output);
echo '<pre>'.print_r($array, true).'</pre>';
?>
感谢帮助!
编辑:
通过与@WagnerVaz 的代码结合
$mystring = "test_command_one";
exec("ps aux | grep 'screen .* $mystring' | grep -v grep | awk '{ print $2 }' | head -1", $out);
print "The PID is: " . $out[0];
试试这个:
<?php
$mystring = "test_command_one";
exec("ps aux | grep \"${mystring}\" | grep -v grep | awk '{ print $2 }' | head -1", $out);
print "The PID is: " . $out[0];
?>
编辑:结合@romaninsh的shell exec
$pid = shell_exec($cmd . " && echo $!");
或者,只需使用:
$pid = getmypid();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.