Home > admin-stuff > Stop a long running shell/bash process after timeout

Stop a long running shell/bash process after timeout

Thu Sep 10 2009 02:00:00 GMT+0200 (Central European Summer Time)

Sometimes a process should just run for a maximum amount of time. A nightly long network transfer, a backup, or a statistical report shouldn't accidentally run until next business hours. A watchdog timer which kills a process once a certain time has passed by is needed. As usual in Shell programming multiple ways are possible, all of them have certain drawbacks. The simplest one would be just to call the long running process in the background and capture its PID. But then it is not that easy to capture the return value of it. Here is my shot with an additional at job:

#!/bin/sh
MY_PID=$$
TIMEOUT=1 # in minutes

# Install at job as watchdog to remove long running process
WATCHDOG_CMDFILE=/tmp/`basename $0`-$MY_PID
echo "# watchdogfile script" > $WATCHDOG_CMDFILE
echo "kill -0 `echo $MY_PID` 2>/dev/null" >> $WATCHDOG_CMDFILE
echo "if [ $? -eq 0 ]; then" >> $WATCHDOG_CMDFILE
echo "  ps -o pid= --ppid `echo $MY_PID` | xargs kill" >> $WATCHDOG_CMDFILE
echo "  echo "long running process aborted because it ran too long"" >> $WATCHDOG_CMDFILE
echo "fi" >> $WATCHDOG_CMDFILE
echo "rm -f `echo $WATCHDOG_CMDFILE`" >> $WATCHDOG_CMDFILE
at -f $WATCHDOG_CMDFILE now + $TIMEOUT min

# Start my very sophisticated long running task
sleep 3600 # 1 hour
RET=$?

# do whatever you normally do after the long running process finishes
echo $RET

(Note that at sends out emails with the stdout/stderr. If you have another notification method to indicate an aborted job, ensure that nothing is printed to std & stderr.)

Categories: admin-stuff
Comments are closed.