home *** CD-ROM | disk | FTP | other *** search
- From: maart@cs.vu.nl (Maarten Litmaath)
- Newsgroups: comp.unix.questions,alt.sources
- Subject: Re: Timeout on shell command.
- Message-ID: <7321@star.cs.vu.nl>
- Date: 16 Aug 90 14:13:25 GMT
-
- In article <3716@sactoh0.UUCP>,
- jak@sactoh0.UUCP (Jay A. Konigsberg) writes:
- )...
- )command & # execute in background
-
- What if the command is supposed to run in the _foreground_?
- The following timeout shell script can be easily converted to a C program
- if desired.
- --------------------cut here--------------------
- #!/bin/sh
- # @(#)timeout 6.2 90/03/01 Maarten Litmaath
-
- prog=`basename $0`
- usage="Usage: $prog [-signal] [timeout] [:interval] [+delay] [--] <command>"
-
- SIG=-KILL # default signal sent to the process when the timer expires,
- # unless a delay option has been given: then it is -TERM
- sigopt=0
- timeout=60 # default timeout
- interval=15 # default interval between checks if the process is still alive
- delay= # (if specified) the delay between posting the given signal and
- # destroying the process (kill -KILL)
-
- while :
- do
- case $1 in
- --)
- shift
- break
- ;;
- -*)
- SIG=$1
- sigopt=1
- ;;
- [0-9]*)
- timeout=$1
- ;;
- :*)
- EXPR='..\(.*\)'
- interval=`expr x"$1" : "$EXPR"`
- ;;
- +*)
- EXPR='..\(.*\)'
- delay=`expr x"$1" : "$EXPR"`
- case $sigopt in
- 0)
- SIG=-TERM
- esac
- ;;
- *)
- break
- esac
- shift
- done
-
- case $# in
- 0)
- echo "$usage" >&2
- exit 2
- esac
-
- (
- for t in $timeout $delay
- do
- while test $t -gt $interval
- do
- sleep $interval
- kill -0 $$ || exit
- t=`expr $t - $interval`
- done
- sleep $t
- kill $SIG $$ && kill -0 $$ || exit
- SIG=-KILL
- done
- ) 2> /dev/null &
-
- exec "$@"
- --
- "UNIX was never designed to keep people from doing stupid things, because
- that policy would also keep them from doing clever things." (Doug Gwyn)
-