This question already has an answer here:

I am using a custom script (stream.sh) which starts on boot. However, I want to check after every 5 minutes if the script is running. If it is not running, I want to run it again. How can I configure it in crontab or else?

I am using Ubuntu 18.04.

marked as duplicate by Jeff Schaller, Thomas, Rui F Ribeiro, GAD3R, X Tian Feb 10 at 18:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Does your script crash or it exits normally? If it exits normally you can set a PID lockfile in /var/run and remove it at exit and check existence of PID lockfile in /var/run in crontab entry. However, if you want to respawn your script.sh immediately after it exits your init system might be a better tool for the job. – Arkadiusz Drabczyk Feb 10 at 10:50
  • 5
    Define a service in systemd that uses your script. You'll get automatic start/restart/stop, logging, etc... – xenoid Feb 10 at 12:02

You may then, if your need is to have it running perpetually, to avoid running it from crontab, run it from inittab with respawn option.


Schedule below mentioned script in crontab

*/5 * * * * /path_of_script


process=`ps -eaf | grep -i scriptname | grep -iv 'grep' | wc -l`
if [[ $process > 0 ]]
echo "script is running"
echo "script is not running"
echo "Need to start the script"
sh scriptname
  • A better way is to have the script create a .pid file in /var/run with its own PID. When the script starts it first checks for the PID file and if found check if the process with that pid is still running. – xenoid Feb 10 at 11:58

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