Keep that command running - keeprunning

I wanted to keep some programs running, which is to say restart them if they crashed, but I also wanted them to be able to exit normally. The platform is Linux, the problem is compiz, the solution is a very small shell script. Surely the internets will let me know if there is something grossly wrong with it. This script is not meant for long-running daemons, there are plenty of tools for that already.

Here 'tis:

#!/bin/bash
#
# keeprunning - keep a process running unless it flaps
#               exits with the status of the child process
#               lets it exit if it wants to
#
declare -i STARTTIME TIME

if [ "x${1}x" == "x-fx" ]; then
  shift
  if [ ! -z "${1##*[!0-9]*}" ]; then
    FLAPTIME=$1
    shift
  else
    echo "-f expects an integer for flap time in seconds"
    exit 1
  fi
fi

if [ "$#" == "0" ]; then
  echo "keeprunning - keep a process running unless it flaps
  usage: keeprunning [-f seconds] <command>"
  exit 1
fi

while true; do

STARTTIME=$(date +%s)
$*
exitstatus=$?
if [ "$exitstatus" -eq 0 ]; then
  exit 0
else
  TIME=${STARTTIME}+${FLAPTIME}
  if [ $TIME -gt $(date +%s) ];
  then
    exit $exitstatus
  else
    echo "Restarting command "$*""
  fi
fi

done

Credit to jilles for how to test for a number with shell builtins. This is actually a sh script, not really a bash script at all. In short, if the command exits with a non-zero status and after more than $FLAPTIME, it is restarted. If it exits with a zero status then it is assumed to have wanted to exit, and if it exits with a non-zero status but within $FLAPTIME, it is assumed to have crashed after a very short period and thusly deserved to have crashed, preventing restart loops. Relaunching also/only on zero status would be an obvious improvement to this script. So would permitting a specified number of restart attempts rather than just giving up.

Add new comment

Default

  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • You may link to images on this site using a special syntax
  • Web page addresses and e-mail addresses turn into links automatically.
  • To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • Filtered words will be replaced with the filtered version of the word.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <q>

Issue

  • Lines and paragraphs break automatically.
  • To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>

Drinking Game

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <p> <br> <pre> <h2> <h3> <h4>
  • Images may be embedded like: [image:node_id align=alignment hspace=n vspace=n border=n size=label width=n height=n nolink=(0|1) class=name style=style-data node=id] Leave off any attributes you don't want.
  • [img_assist|...] tags will be displayed, maybe. Please don't make more of them.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.