How to keep a job running in Linux

25 October 11

There are many ways to keep a process running on linux but I haven't seen any that are as easy to implement as the script below.

Basically the script does a ps ax and then a grep for your process. If it's not running it will re-start the process.

You install the script into your crontab i.e. crontab -e

As a bonus this mechanism will re-start your process after a re-boot.

Edit your crontab

Cut and paste the following code into your crontab.

*/5 * * * * /home/path_to_the_script/make-run.sh

Make sure the cron entry is pointing to where your make-run.sh is located.

The Bash script

#!/bin/bash 
# make-run.sh 
# make sure a process is always running.  
# Add the following to the crontab (i.e. crontab -e)
# */5 * * * * /home/path_to_make_run/make-run.sh

process=servermonitoringhq 
makerun="/home/path_to_the_job_you_want_running/runjob.sh"  

if ps ax | grep -v grep | grep $process > /dev/null         
then                 
  exit         
else         
  $makerun &
fi 

Test the script on it's own first to make sure it starts your job.

Server Monitoring HQ

Spot server issues in lightning time with no software to install.

Screenshot

"It's like PingDom for servers"