简体   繁体   中英

Bash script to check a specific php process is running?

I am running websocket server with the nohup command like below.

nohup php -q chat-server.php > ratchet_ws.log &

But it stop working after few hours.So i have created a bash script like below

#!/bin/bash
while true
do
if pgrep php > /dev/null
then
    echo  "Running" 
else
   cd /..path../
   nohup php -q chat-server.php > ratchet_ws.log &
fi
sleep 1;
done

Then i run the script with below command like below.

nohup sh chk_process.sh > chk_process.log &

The work of the above script if to check my chat-server.php is running or not if it is not running then it will start it again.

if pgrep php > /dev/null

But this line is checking any php process is running or not .But i want it to check only my chat-server.php.

you can use pgrep -f or pgrep -x option.

entry from man page

-f The pattern is normally only matched against the process name. When -f is set, the full command line is used.
-x Only match processes whose name (or command line if -f is specified) exactly match the pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM