source: vendor/bash/3.1-p17/examples/scripts/timeout

Last change on this file was 3228, checked in by bird, 18 years ago

bash 3.1

File size: 1.2 KB
Line 
1#Newsgroups: comp.unix.admin,comp.unix.solaris,comp.unix.shell
2#From: gwc@root.co.uk (Geoff Clare)
3#Subject: Re: timeout -t <sec> <unix command> (Re: How to give rsh a shorter timeout?)
4#Message-ID: <EoBxrs.223@root.co.uk>
5#Date: Fri, 13 Feb 1998 18:23:52 GMT
6
7#
8# Conversion to bash v2 syntax done by Chet Ramey <chet@po.cwru.edu
9# UNTESTED
10#
11
12prog=${0##*/}
13usage="usage: $prog [-signal] [timeout] [:interval] [+delay] [--] <command>"
14
15SIG=-TERM # default signal sent to the process when the timer expires
16timeout=60 # default timeout
17interval=15 # default interval between checks if the process is still alive
18delay=2 # default delay between posting the given signal and
19 # destroying the process (kill -KILL)
20
21while :
22do
23 case $1 in
24 --) shift; break ;;
25 -*) SIG=$1 ;;
26 [0-9]*) timeout=$1 ;;
27 :*) EXPR='..\(.*\)' ; interval=`expr x"$1" : "$EXPR"` ;;
28 +*) EXPR='..\(.*\)' ; delay=`expr x"$1" : "$EXPR"` ;;
29 *) break ;;
30 esac
31 shift
32done
33
34case $# in
350) echo "$prog: $usage" >&2 ; exit 2 ;;
36esac
37
38(
39 for t in $timeout $delay
40 do
41 while (( $t > $interval ))
42 do
43 sleep $interval
44 kill -0 $$ || exit
45 t=$(( $t - $interval ))
46 done
47 sleep $t
48 kill $SIG $$ && kill -0 $$ || exit
49 SIG=-KILL
50 done
51) 2> /dev/null &
52
53exec "$@"
Note: See TracBrowser for help on using the repository browser.