And it runs and runs and runs…

In the style of an old German Car-Spot, one often needs to make sure that a process get’s restarted as soon as it quits due to some errors.

As root, the simplest way to achieve this would be an entry into “/etc/inittab”, but as a user those tasks can be quite a mess to do. Some years ago, the f3l-team solved it in some hackish and not that stylish way by the following snippet (the task was to sync directories, even though the internet-connection over DSL might get lost):

#!/bin/bash
# This is some ugly hack, see below for better solution!

function sync2me {
  rsync --partial-dir=.sync -rzve 'ssh -p 22' --size-only --progress --exclude ".*" root@host:<src-dir> <target-dir>
}

while true; do
  sync2me
  if [ $? != 0 ] ; then
    break
  fi
done

As can be seen easily, the sync-process runs and starts a new run as soon as sync2me stops (in fact, I haven’t really understood what the if-clause might be for, as this the while-loop starts a new run of sync2me no matter what…). This script worked most of the time, but I didn’t feel good using it.

Later, while trying to get Felix “Fefe” von Leitner’s “Fnord-httpd” online on my homeserver, I found “daemontools”, “a collection of tools for managing UNIX services” written by Daniel J. Bernstein (if you haven’t heard that name before, wait, I’ll google that for you), which should be available for almost all linux-distributions. This program-suite ships a tool called “supervise” that does exactly what I wanna do! It

  • runs a command (that must be called “run”, ok, that’s a litle bit annoying) all over again, restarting it if it fails or exits
  • waits at least one second before starting the next instance (this will prevent System-Hangup, i.e.)
  • checks whether another instance of supervise already runs the given script (which wasn’t the case with our old script)
  • allows to manage multiple independent scripts as if called with supervise by using the “svscan”-command

and even more, see man-pages for more details.

If you want to run multiple services without the need for own directories, “run” files and so on, take a look at the Supervisord-Project, but in most scenarios I think this would be like using a sledgehammer to crack a nut…

Written by pheerai on 27/08/2013 Categories: Free/Open Source Software, International corner Tags: ,
No Comments on And it runs and runs and runs…

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * * Time limit is exhausted. Please reload the CAPTCHA.