From feb92a5e5e06964c03038b9843376f48cea55ce6 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 2 Nov 2013 22:58:39 -0400 Subject: offlineimap-runner: this is the version I've been running --- offlineimap-runner.sh | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/offlineimap-runner.sh b/offlineimap-runner.sh index 189bda3..e4a6f6c 100644 --- a/offlineimap-runner.sh +++ b/offlineimap-runner.sh @@ -14,15 +14,22 @@ # minute to run, then the instance invoked by cron would bork everything. # As awesome as this is, it has one fatal flaw: an instance hanging. -# I've only had this happen once, but an instance of offlineimap invoked by cron -# decided to hang up. After 2 days of recieving suspiciously little email, I -# decided to check if offlineimap was running ok. That instance had hung up, not -# recieving any mail, but preventing cron from starting another instance. +# This only happens rarely, but I've had an instance of offlineimap invoked by +# cron decided to hang up. After 2 days of recieving suspiciously little email, +# I decided to check if offlineimap was running ok. That instance had hung up, +# not recieving any mail, but preventing cron from starting another instance. # # So, here is my workaround: keep track of how many times in a row that we've # tried to start offlineimap, and it failed. If that number exceeds a specified # number, then assume the running instance hung, and kill it. +if [ "$1" = '-v' ]; then + shift; + msg() { echo "$@"; } +else + msg() { :; } +fi + # A note on notation: # the number/limit is the number of failures allowed *before* anything is killed. # So, if the limit is "1", the first time, it will just pass by. The second @@ -30,31 +37,34 @@ # kill it. default_limit=2 limit=${1-$default_limit} +shift cookie_file="$HOME/.offlineimap.cookie" offlineimap='offlineimap-exit' # Try running offlineimap -if $offlineimap; then +if $offlineimap "$@"; then # Everything went smoothly, remove any record of failures + msg 'Successfully ran offlineimap' + msg 'Clearing cookie file' echo '0' > "$cookie_file" else # oh noez! + msg 'offlineimap had an error' # we interupt your regularly scheduled panic for a sanity check if [ ! -f "$cookie_file" ]; then echo '0' > "$cookie_file" fi # you may now resume panicing number=`cat "$cookie_file"` + echo $(($number+1)) > "$cookie_file" + + msg " has failed $number times (with a limit of $limit)" if [ "$number" -ge "$limit" ]; then # kill it! - # extract the first argument from our offlineimap command - set -- $offlineimap - killall $1 + killall -9 offlineimap # We invoke offlineimap directly, instead of recursing on # ourself because if something else is causing it to fail, then # we'll fork-bomb - $offlineimap - else - echo $(($number+1)) > "$cookie_file" + $offlineimap "$@" fi fi -- cgit v1.2.3