Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a worker program which I start in rc.local. The program is not the most stable in the world so I would like to set up a watcher of some kind that checks to see if the aforementioned program is chugging along and if not, start up the program again.

I can imagine a couple of ways to do this, although I am sure there are more:

  1. have a cron job that runs and looks for the running program. If there, it stops, otherwise it fires off another instance of the program
  2. run a dedicated monitoring program that runs all the time and polls frequently to see if the program is running.

Do any of you have examples of either of these implementations or recommendations on how I should do this?

share|improve this question

closed as too localized by fossfreedom Jun 6 '12 at 22:42

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Today I was notified that this question has 2500 views and I earned a "Notable Question" badge. This seems juxtaposed to the logic behind closing the question. Empirically this question is interesting for future visitors. –  JD Long Nov 28 at 15:29

2 Answers 2

I'd suggest Monit. You can install it from Synaptic or sudo apt-get install monit. I use it on my server to monitor my SSH, Apache, etc and restart them if they fail.

share|improve this answer

You might be able use a helper script that runs the process in an infinite loop, e.g.:

#!/bin/bash

# Infinite loop
while true; do

    # Get starting time
    start_epoch=$(date +%s)

    # Run program
    my-program

    # Abort if the application exited too quickly
    duration=$(( $(date +%s) - $start_epoch ))
    if [[ "$duration" < 30 ]]; then
        echo "Program exited too quickly. Aborting relauncher."
        exit
    fi

done

exit

Call this script instead of my-program in /etc/rc.local.

For more complex example, see this script which I use to automatically restart the NetworkManager Applet.

share|improve this answer

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