我在我的linux服务器(Ubuntu)上运行一个命令。例如:
screen -A -h 1500 -m -dmS test_command_one /home/SKY-users/SKY-001/./script有没有办法控制这个后台进程的PID,这个进程的屏幕名是:test_command_one
ps aux | 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>';
?>感谢您的帮助!
发布于 2011-10-20 06:38:03
编辑:
通过与@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];解释
发布于 2011-10-20 06:44:34
试试这个:
<?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相结合
发布于 2012-04-03 19:07:30
你也可以尝试这样做:
echo exec('pidof test_command_one');它更短;)
另请参阅:pidof manpage
https://stackoverflow.com/questions/7829005
复制相似问题