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:


var=$(pgrep -fl my-php-program.php)
if [ -z "$var" ]
  echo "Not running"
  echo "Running!"

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.

  • 1
    Zombie processes happen because, although the program has exited, there's still a resource (the exit status) that must benefit harvested (wait, waitpid) by the parent process. Rather than chasing zombies, fix the parent process that starts the eventual zombies. – waltinator Oct 29 '20 at 4:16
  • 1
    Does this answer your question? What does grep -v "grep" mean and do? and specially this given answer – αғsнιη Oct 29 '20 at 6:18
  • @waltinator, thankk you. I will take a look at a solution based on your suggestion. Regards – Geppettvs D'Constanzo Oct 29 '20 at 16:23
  • @αғsнιη Thank you.I knew that, I was trying to get a solution by excluding the lines which contains the process itself, as I said: unsuccessfully. – Geppettvs D'Constanzo Oct 29 '20 at 16:25

Since pgrep uses an Extended Regular Expression, you could use a regexp that doesn't match itself, e.g.

pgrep '[m]y_php_program.php'

Read man pgrep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.