Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm looking for a way to monitor a process, and relaunch the executable if the process dies for whatever reason. Does Linux have a built-in tool that can do this?

share|improve this question
    
there's some more answers and pointers in this question serverfault.com/questions/56526/… –  jrg Aug 27 '09 at 10:53
add comment

6 Answers 6

up vote 7 down vote accepted

If it's launched from init, you can have it respawned by init. Set the action for the process to 'respawn' for the run levels you want the process at.

share|improve this answer
    
Similarly, launchd is pretty nice. –  Nerdling Aug 27 '09 at 11:56
    
Aye. Daemon tools is pretty cool for this kind of stuff as well, but fails the 'built in' test, unforuntately. –  Cian Aug 27 '09 at 12:04
add comment

Do you only want to restart it if it dies? The problem with this is that it doesn't handle situations where it freezes. So to just check for the process won't always help. So if this is something like a web server, you want to have a script checking it from the user's perspective.

If you set up Nagios Monitoring, you can then use event handlers. These are just scripts that you write that will run when the service down, so you could have one that restarts something like Apache if the web site is down.

share|improve this answer
    
+1, Be sure that you also cover situations where it is intentionally killed too. –  Jimmy Shelter Aug 27 '09 at 13:38
    
Good point, though in my case the process just disappears –  pufferfish Aug 27 '09 at 14:06
add comment

I have answered a similar question before.

In your case:

#!/bin/bash
while ! <<command>>
do
  sleep 1
  echo "Restarting program..."
done

Replace <<command>> with the command you want to execute. The process has to finish with exit code zero in order to break the loop. Otherwise, it is restarted by the script.

share|improve this answer
    
Nice. This works with ksh as well, and I'd change the "echo" to a "logger" command to put the message into the system logs. –  Mei Aug 27 '09 at 16:23
add comment

Depending on the distro, but we've successfully used "monit" for this task. It's pretty easy. You can write your own checks, monitor the proces PID, etc.

Example monitrc file:

check process sshd with pidfile /var/run/sshd.pid
   start program  "/etc/init.d/ssh start"
   stop program  "/etc/init.d/ssh stop"
   if failed port 22 protocol ssh then restart
   if 5 restarts within 5 cycles then timeout
share|improve this answer
add comment

Why not a simple bash script?

#!/usr/bin/bash
while `true`
do
  xeyes
done

Replace xeyes with program of your choice.

share|improve this answer
add comment

Short Answer: No you have to do it yourself.

Long Answer: You'll need a parent process to start it, and then keep checking that it's alive and restart it if needed.

You could use something like daemontools to manage it.

share|improve this answer
    
Short answer: you're dead wrong. Long answer: if a Linux distro doesn't have inittab, it will have /etc/event.d/ - either mechanism will restart a process when it dies. –  Paul Tomblin Aug 27 '09 at 12:21
add comment

Your Answer

 
discard

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

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