Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!uunet!mcsun!ukc!stc!root44!gwc From: gwc@root.co.uk (Geoff Clare) Newsgroups: comp.unix.questions Subject: Re: Killing the correct process Message-ID: <1212@root44.co.uk> Date: 9 Feb 90 10:50:31 GMT References: <22332@adm.BRL.MIL> <5312@star.cs.vu.nl> Reply-To: gwc@root.co.uk (Geoff Clare) Organization: UniSoft Ltd, London, England Lines: 75 In article <5312@star.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes: >In article <22332@adm.BRL.MIL>, > marwood@ncs.dnd.ca (Gordon Marwood) wants to timeout ftp. > >How about using the following general purpose script? If that's out of the >question, the script might still give you a hint how to solve your problem. [ extremely complicated script deleted ] There is a much simpler way than Maarten's. The basic strategy is: (sleep $time; kill $$) & exec "$@" Here is my "timeout" command, using this method with added frills. An important feature which Maarten's script lacks, is that mine kills the process with SIGTERM, allowing it to clean up. It only goes for SIGKILL if the SIGTERM doesn't do the job. Maarten dives straight for SIGKILL, which could leave a mess. ---------------------- cut here ---------------------- : # execute a command with timeout # Geoff Clare , Feb 1990 USAGE="usage: $0 [-v] [seconds] command args ..." SIGKILL=9 # may be system dependant time=10 # default 10 seconds verbose=n for i in 1 2 do case $1 in -v) verbose=y shift ;; [0-9]*) time=$1 shift ;; ""|-*) echo >&2 "$USAGE" exit 1 ;; esac done pid=$$ ( sleep "$time" # use SIGTERM first to allow process to clean up kill $pid >/dev/null 2>&1 rc=$? sleep 2 # if process hasn't died yet, use SIGKILL kill -$SIGKILL $pid >/dev/null 2>&1 case "$rc$verbose" in 0y) echo >&2 " TIMED OUT \"$*\"" ;; esac ) & exec "$@" ---------------------- cut here ---------------------- -- Geoff Clare, UniSoft Limited, Saunderson House, Hayne Street, London EC1A 9HH gwc@root.co.uk (Dumb mailers: ...!uunet!root.co.uk!gwc) Tel: +44-1-315-6600 (from 6th May 1990): +44-71-315-6600