Services – Automatically restart a Unix job if it goes down?

I have a job that I would like to “daemonize” on Unix: I want it to come up when the computer boots, and I want it to restart if it goes down.

A simple way to do this is to setup a cronjob that runs every 10 or 20 minutes. The cronjob should restart the application if it’s not already running.

How do I write this last part of the script:”If the job is not currently running, then start the job”?

Solution:

If your program runs in the foreground, use Gerrit Pape’s runit. Advantages:

  • Its pretty well bullet proof (based on Dan Berstein’s daemontools).
  • It runs on a wide variety of platforms (portable).
  • It is packaged on Ubuntu and Debian (along w/ above..).
  • It is relatively easy to configure (run script, log script, some symlinks).