restartWrapper is wonderful for protecting against the possibility of a fluke crash, but if something gets legitimately broken that a restart *can't* fix, I don't want to be inundated with email every few seconds. So I have modified restartWrapper to also accept a -min_interval parameter which specifies the minimum time that must elapse between two successive restarts (defaults to zero if not provided).

So "restartWrapper -delay 1 -min_interval 300 prog" will restart prog either 1 second after the previous run stopped OR 5 minutes after the previous run started, whichever is later.

The attached patch is against the Radiator 4.7 version of restartWrapper. Hopefully others will find it helpful too.

David
--- restartWrapper.orig 2010-10-26 11:06:32.000000000 -0500
+++ restartWrapper      2010-10-26 11:52:57.000000000 -0500
@@ -12,6 +12,9 @@
               "h",         # Help, show usage
               "delay=n",   # Delay between restarts, seconds
                            # default: 10
+              "min_interval=n", # Minimum interval between
+                                # successive restarts (secs)
+                                # default: 0
               "mail=s",    # Address to email notification to
                            # default: no email
               "sendmail=s", # Path to sendmail prog
@@ -31,6 +34,11 @@
 $delay = 10;
 $delay = $opt_delay if defined $opt_delay;
 
+# How long after the previous START time to wait
+# before restarting the prog
+$min_interval = 0;
+$min_interval = $opt_min_interval if defined $opt_min_interval;
+
 # Where to find sendmail so we can send mail
 $sendmail = '/usr/lib/sendmail';
 $sendmail = $opt_sendmail if defined $opt_sendmail;
@@ -48,10 +56,14 @@
 
 while (1)
 {
+    $start_time = time;
     $stderr_output = `$command 2>&1 1>/dev/null`;
     $exit_signal_num  = $? & 127;
     $exit_dumped_core = $? & 128;        
     $exit_status = $? >> 8;
+    # wait time must satisfy both delay and min_interval
+    $wait = $min_interval - (time - $start_time);
+    $wait = $delay if $wait < $delay;
     if (defined $opt_syslog)
     {
        open (LOGGER, "| $logger -t$0 -p $syslog >/dev/null 2>&1") 
@@ -77,7 +89,7 @@
 
 The STDERR output was $stderr_output.
 
-The program will be restarted again by $0 in $delay seconds.
+The program will be restarted again by $0 in $wait seconds.
 
 ==================================================================
 This mail message was automatically generated by restartWrapper,
@@ -88,13 +100,14 @@
         close (MAIL);
        warn "$0: error running $sendmail" if $?;
     }
-    sleep $delay;
+    sleep $wait;
 }
 
 #####################################################################
 sub usage
 {
     print "usage: $0 [-h] [-delay n] [-mail address] 
+ [-min_interval n (default: 0)]
  [-sendmail path-to-sendmail] 
  [-syslog facility.level (default: user.err)]
  [-logger path-to-logger (default: /usr/bin/logger)]
_______________________________________________
radiator mailing list
[email protected]
http://www.open.com.au/mailman/listinfo/radiator

Reply via email to