Created
April 7, 2015 17:07
-
-
Save oxpa/dbacf269e5a9b43505de to your computer and use it in GitHub Desktop.
run with timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
WORKING_DIR=`dirname $0` | |
prog=${0##*/} | |
usage="usage: $prog [-signal] [timeout] [:interval] [+delay] [--] <command>" | |
SIG=-TERM # default signal sent to the process when the timer expires | |
timeout=60 # default timeout | |
interval=15 # default interval between checks if the process is still alive | |
delay=2 # default delay between posting the given signal and | |
# destroying the process (kill -KILL) | |
while : | |
do | |
case $1 in | |
--) shift; break ;; | |
-*) SIG=$1 ;; | |
[0-9]*) timeout=$1 ;; | |
:*) EXPR='..\(.*\)' ; interval=`expr x"$1" : "$EXPR"` ;; | |
+*) EXPR='..\(.*\)' ; delay=`expr x"$1" : "$EXPR"` ;; | |
*) break ;; | |
esac | |
shift | |
done | |
case $# in | |
0) echo "$prog: $usage" >&2 ; exit 2 ;; | |
esac | |
( | |
for t in $timeout $delay | |
do | |
while (( $t > $interval )) | |
do | |
sleep $interval | |
kill -0 $$ || exit | |
t=$(( $t - $interval )) | |
done | |
sleep $t | |
kill $SIG $$ && kill -0 $$ || exit 3 | |
SIG=-KILL | |
done | |
) 2> /dev/null & | |
exec "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment