#!/bin/bash
SIG=-TERM # default signal sent to the process when the timer expires
timeout=4 # default timeout
interval=1 # default interval between checks if the process is still alive
delay=2 # default delay between posting the given signal and
# destroying the process (kill -KILL)
case $# in
0) echo "$prog: $usage" >&2 ; exit 2 ;;
esac
(
for t in $timeout $delay
do
while (( $t > $interval ))
do
sleep $interval
kill -0 $$ || exit
t=$(( $t - $interval ))
done
sleep $t
kill $SIG $$ && kill -0 $$ || exit
SIG=-KILL
done
) 2> /dev/null &
trap "exit 0" SIGKILL SIGTERM
echo $$
exec "$@"