开发者

Determining if a process is running by its Command Name [duplicate]

This question already has answers here: Linux / Bash, using ps -o to get process by specific name? (5 answers) Closed 9 years ago.

So I have multiple processes running: N amount of working threads and 1 collector thread.

I want to determine ONLY if the coll开发者_StackOverflowector thread is active.

These are the command results from 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

I want to be able to just determine if the collector is running. How can i achieve this?

I plan to have a cron job checking this every few minutes and then executing the script if it dies.


Ah.. durp. Forgot I could just run that into a variable in php and sub string it....

Solution:

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 &");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜