Determining if a process is running by its Command Name [duplicate]
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 &");
}
精彩评论