Take the 2-minute tour ×

If my software has aborted because of an error, is there a way so that my Ubuntu will right away re-open it? I'm using g++ sample.cpp -o sample and ./sample

But if it crashes it's really a problem for me. What would be the solution? Thanks.

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

A simple bash script?

#! /bin/bash

until ./my_app; do
    sleep 2 # To prevent insanity
share|improve this answer
Better than the accepted answer because it gives the program the ability to willingly exit. –  Dessix Machina Sep 8 '11 at 0:45
+1 This is the kind of script everyone should have handy. –  Iterator Sep 8 '11 at 0:56
I'm using ubuntu and doing this root@monstenation:~# !/bin/bash -bash: !/bin/bash: event not found Why is that? –  Eli Sep 8 '11 at 13:39
add comment

Simple solution would just be to make a shell script to start the program every time it finishes execution.


while true

You may want to save a log file or the program output each time sample finishes execution.

share|improve this answer
One possible problem with this (and some of the other solutions) is that if something goes wrong that causes the program to crash quickly every time you start it, you'll be running it many many times. You might consider adding a sleep to the loop. –  Keith Thompson Sep 8 '11 at 1:05
Save the contents into a script, ex foo.sh. Then make it executable with chmod +x foo.sh. Then execute like so: ./foo.sh. Alternatively you can enter each line at the bash prompt, just leave off the first line (although you should probably save it as a script if you plan on running it again). –  Gavin H Sep 8 '11 at 15:30
add comment

Another approach is to have a master daemon which gets frequent heart beats from the program which you want to monitor and when heart-beats are not received, start the program which is being watched.

share|improve this answer
add comment

Possibly overkill for what you're after, it's hard to tell, but the God monitoring framework might help - a little more advanced and feature-ful than a simple while(1)

share|improve this answer
Good god (no pun intended, but...), that is one heck of a system - it stops short of rewriting an entire shell (and may do things a shell cannot). –  Iterator Sep 8 '11 at 0:55
Well yes, I did say it might be a little overkill ;) –  Kristian Glass Sep 8 '11 at 1:01
add comment

Your Answer


