2011-09-08 78 views
0

所以我有多个进程在运行:N个工作线程和1个收集器线程。确定一个进程是否按命令名运行

我只想确定收集线程是否处于活动状态。

这些都是从pgrep php | xargs ps

PID TTY  STAT TIME COMMAND 
1682 ?  S  0:00 php /var/www/robot/twitterbot.php -t tokenstring -s tokensecretstring 
3744 ?  S  0:00 php /var/www/robot/twitterbot.php -t tokenstring -s tokensecretstring 
4972 ?  S  0:00 php /var/www/robot/twitterbot.php -t tokenstring -s tokensecretstring 
5215 pts/0 S  0:00 php twitterbot-collector.php 

命令的结果我想能够只是确定收集器运行。我怎样才能做到这一点?

我打算每隔几分钟检查一次cron作业,然后在脚本死亡的情况下执行脚本。

回答

0

Ah .. durp。忘了我可以只运行到PHP和子字符串,一个变量....

解决方案:

exec("pgrep php | xargs ps", $result, $r2); 
$found = false; 
foreach($result as $r){ 
    if(strstr($r, 'twitterbot-collector')){ 
     $found = true; 
    } 
} 

if(!$found){ 
    system("php /var/www/robot/twitterbot-collector.php > /dev/null 2>/dev/null &"); 
} 
相关问题