开发者

Checking for an active process in linux

I've got a rather buggy开发者_如何学运维 version of php-fpm (ubuntu) running, I'm looking for a script (which will be run as cronjob).. that check if php5-fpm is running, and if its not.. to re-execute it.


You can check if a process is running using the pgrep command.

victor:~$ pgrep bash
5554
victor:~$ 

As you can see, it returns the process ID of all matching processes.


You may use the ps command to detect running processes and grep for the 'php-pfm' application - could be looking like that:

TEST=`ps -faxu |grep 'php-pfm'|grep -v 'grep'`
if [ -e  "$TEST" ]; then
    #not running... restart!
    php-fpm &
fi

You may try the man pages for 'ps' and 'grep'...

Edit: new code fragment including victors fix:

TEST=`ps -faxu |grep 'php-pfm'|grep -v 'grep'`
if [ -z  "$TEST" ]; then
    #not running... restart!
    php-fpm &
fi

-- Mayday

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜