Timeout on shell command.

Donald Lashomb donlash at uncle.uucp
Mon Aug 13 08:16:58 AEST 1990


In article <BRISTER.90Aug10222433 at westworld.decwrl.dec.com> brister at decwrl.dec.com (James Brister) writes:
>I'd like to have a shell script run a command, but if that command doesn't
>finish in X seconds, then the script should kill it, if the command
>finishes sooner then the script should immediately continue. Any ideas on
>how one could achieve this?

I'd approach it like this-

#!/bin/sh
#
#  run the command in the background
#  remember its process ID
#  sleep for X seconds
#  kill the background command --- note: if
#  the background command is finished, then
#  the kill will fail benignly.
#
command &
cmdpid=$!
sleep $X
kill $cmdpid

hope this helps
-Don



More information about the Comp.unix.questions mailing list