diff --git a/etc/cron.d/anacron b/etc/cron.d/anacron index 7600d6b..db96fcc 100644 --- a/etc/cron.d/anacron +++ b/etc/cron.d/anacron @@ -4,3 +4,4 @@ SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 30 7 * * * root initctl emit start-anacron-from-cron +35 7 * * * root /usr/lib/anacron/anacron-isstuck diff --git a/usr/lib/anacron/anacron-isstuck b/usr/lib/anacron/anacron-isstuck new file mode 100755 index 0000000..86f0897 --- /dev/null +++ b/usr/lib/anacron/anacron-isstuck @@ -0,0 +1,37 @@ +#!/bin/sh + +# Check from cron if anacron has been running a long time + +# Let's say if anacron runs longer than 12h it's a problem (in seconds) +too_long=43200 + +# Don't continue if not an upstart system +test -x /sbin/initctl || exit 0 +/sbin/initctl --version | grep -q upstart || exit 0 + +# Don't continue if anacron is not currently running +stat="`initctl status anacron`" +echo $stat | grep -q start/running || exit 0 + +# Retrive the PID from upstart status +pid=`echo $stat | awk '{print $4}'` +if [ -z "$pid" ] ; then + echo Anacron seems to be running, but could not determine its PID + exit 2 +fi + +# Not sure how portable this syntax is. +ps_start=`ps h p$pid olstart` + +# Get now & process start time as seconds since epoch +now_ts=`date +%s` +start_ts=`date +%s -d "$ps_start"` +duration=`expr $now_ts - $start_ts` + +if [ $duration -gt $too_long ] ; then + echo Warning: Anacron has been running for more than `expr $duration / 3600` hours. + echo There may be a stuck job. + exit 1 +fi + +exit 0