Under Linux what would be the best way for a program to restart itself on a crash by catching the exception in a crashhandler (for example on a segfault)?
Processes can't restart themselves, but you could use a utility like crontab(1)
to schedule a script to check if the process is still alive at regular intervals.
simplest is
while [ 1 ]; do ./program && break; done
basically, you run program until it is return 0, then you break.
You can have a loop in which you essentially fork()
, do the real work in the child, and just wait on the child and check its exit status in the parent. You can also use a system which monitors and restarts programs in a similar fashion, such as daemontools, runit, etc.
The program itself obviously shouldn't check whether it is running or not running :)
Most enterprise solutions are actually just fancy ways of grepping the output from ps()
for a given string, and performing an action in the event that certain criteria are satisfied - i.e. if your process is not found, then call the start script.
SIGSEGV
can be caught (see man 3 signal
or man 2 sigaction
), and the program can call one of the exec
family of function on itself in order to restart. Similarly for most runtime crashes (SIGFPE
, SIGILL
, SIGBUS
, SIGSYS
, ...).
I'd think a bit before doing this, though. It is a rather unusual strategy for a unix program, and you may surprise your users (not necessarily in a pleasant way, either).
In any case, be sure to not auto-restart on SIGTERM
if there are any resources you want to clean up before dying, otherwise angry users will use SIGKILL
and you'll leave a mess.
As a complement to what was proposed here:
Another option is to do like it is done for getty daemon. Please see /etc/inittab and appropriate inittab(5) man page. It seems it is most system-wide mean ;-).
It could look like file fragment below. Obvious advantage this mean is pretty standard and it allows to control your daemon through run levels.
# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6