Stop a long running shell/bash process after timeout
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 &quot;# watchdogfile script&quot; &gt; $WATCHDOG_CMDFILE echo &quot;kill -0 `echo $MY_PID` 2&gt;/dev/null&quot; &gt;&gt; $WATCHDOG_CMDFILE echo &quot;if [ $? -eq 0 ]; then&quot; &gt;&gt; $WATCHDOG_CMDFILE echo &quot; ps -o pid= --ppid `echo $MY_PID` | xargs kill&quot; &gt;&gt; $WATCHDOG_CMDFILE echo &quot; echo &quot;long running process aborted because it ran too long&quot;&quot; &gt;&gt; $WATCHDOG_CMDFILE echo &quot;fi&quot; &gt;&gt; $WATCHDOG_CMDFILE echo &quot;rm -f `echo $WATCHDOG_CMDFILE`&quot; &gt;&gt; $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.)