Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a program that should run all the time (it collects some statistics) after Xorg start. However, there can occur some exceptions in this program, or it can be unintentionally closed, and what I want is to automatically restart it. The first idea is to run every minute another script with cron, which will restart it, but this doesn't seem optimal, so what's the preferred method of doing this?

share|improve this question
Can you give us some extra info? For instance: is the program supposed to perform a service, so that it cannot even tolerate a 5 minute gap? Is is performing some computation, so that it can tolerate a 5-minute gap, but must be fed some complex input upon start? –  MariusMatutiae Jan 20 '14 at 10:14

2 Answers 2

There are many ways of doing this and which one you choose depends on how the script is being run. An easy way that works for any command and does not require root access is to run something like this:

while true; do pgrep -f myscript.sh >/dev/null || myscript.sh; sleep 1; done

This will run for ever (while true) and launch myscript.sh if it is not already running.

share|improve this answer

sounds like a good use case for monit: http://mmonit.com/monit

also the same question with more info here:

Automatically restart a Unix job if it goes down?

share|improve this answer
Welcome to Super User! While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. –  terdon Jan 20 '14 at 17:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.