I am trying to write a bash script which, once the user run it, checks if another process is running and show it on screen.
Of course, the script is intended for a different purpose, like sending an email once the script ends satisfactorily, or using a different way to send a message to the user.
The main purpose of this script will be to detect zombie processes which kept running for a certain amount of time and inform to the user to take some actions.
I am trying to check if a php
script is running in the command line, so I have tried using this for checking if the script is running:
detector.sh
#!/bin/bash
var=$(pgrep -fl my-php-program.php)
if [ -z "$var" ]
echo "Not running"
else
echo "Running!"
fi
The script works, and it shows the contents of the running processes, after which I do some other if...else...fi
validations with the $var
variable contents to do something.
Nevertheless, if I run the process constantly using:
while true; do bash detector.sh; sleep 1; done;
The script detects itself and sometimes shows some positive false responses.
I have -unsuccessful- tried this to solve it:
var=$(pgrep -fl my-php-program.php | grep -v php)
Is it there another way to get this problem solved?
Thanks in advance.
P.S.: I don't want to solve this problem in PHP. I would appreciate a lot your advice for bash based solutions only.
Since
pgrep
uses an Extended Regular Expression, you could use a regexp that doesn't match itself, e.g.Read
man pgrep
.