Skip to content
Advertisement

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement