0

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.

1
  • Please be aware that you are checking if a process is running, but a running process isn't always a healthy process nor a responsive process. I would recommend trying to connect to the service as well. Commented Aug 13, 2015 at 6:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @user258367.Can you give an example.
pgrep -f "$COMMAND LINE" or pgrep -x $processname. you can check commandLine or processname by running command ps -x in your session.
Thanks @user258367.I have used "if pgrep -f php -q chat-server.php > /dev/null" its working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.