shell scripting, how to auto-timeout?

Nerius Landys nlandys at gmail.com
Thu Jan 22 14:51:33 PST 2009


> #!/bin/sh
>
> java()
> {
>        echo 'start'
>        sleep 5
>        echo 'stop'
> }
>
> sleep 1 && kill $$ &
> java
> kill $!
>> {
>        echo 'start'
>        sleep 5
>        echo 'stop'
> }
>
> sleep 1 && kill $$ &
> java
> kill $!

That is very genious.  However, I had to add an "exec" to the parent
script.  Here is the test parent script, see the exec line below:

#!/bin/sh
cd `dirname "$0"`
THIS_SCRIPT_PROCESS="$$"
sleep 5 && echo "killing parent script, PID $THIS_SCRIPT_PROCESS" &&
kill "$THIS_SCRIPT_PROCESS" &
TERMINATOR_PROCESS="$!"
exec ./child_script
echo "killing terminator process, PID $TERMINATOR_PROCESS" && kill
"$TERMINATOR_PROCESS"


And here is the child script "child_script":

#!/bin/sh
echo "start"
while true; do
  # Infinite loop; use some CPU so that it's easy to find this in the
output of top.
  echo "foo" > /dev/null
done
echo "stop"


Without the "exec" in the parent script, the parent's child is not
killed when the parent is killed.


More information about the freebsd-questions mailing list