$scriptName [-t timeout] [-i interval] [-d delay] command
Execute a command with a time-out.
Upon time-out expiration SIGTERM (15)
is sent to the process. If SIGTERM
signal is blocked, then the subsequent SIGKILL (9)
terminates it.
-t
timeout
Number of seconds to wait for command completion.
Default value: $DEFAULT_TIMEOUT
seconds.
-i
interval
Interval between checks if the process is still alive.
Positive integer, default value: $DEFAULT_INTERVAL
seconds.
-d
delay
Delay between posting the SIGTERM
signal and destroying the process by SIGKILL
. Default value: $DEFAULT_DELAY
seconds.
As of today, Bash does not support floating point arithmetic (sleep does), therefore all delay/time values must be integers.
Not sure where I got this first, but it's also available here
timeout 5 /path/to/slow/command with options
CoreUtils isn't always available
#!/bin/sh
( your_command ) & pid=$!
( sleep $TIMEOUT && kill -HUP $pid ) 2>/dev/null & watcher=$!
wait $pid 2>/dev/null && pkill -HUP -P $watcher