Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 22 Jan 2009 14:51:31 -0800
From:      Nerius Landys <nlandys@gmail.com>
To:        Maxim Khitrov <mkhitrov@gmail.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: shell scripting, how to auto-timeout?
Message-ID:  <560f92640901221451j2e2b259bw1559a8c8d8912941@mail.gmail.com>
In-Reply-To: <26ddd1750901221333x5356f4f3l6b6410fc05d4e6d4@mail.gmail.com>
References:  <560f92640901221241y4fc1620aree083a812c1f3c8d@mail.gmail.com> <26ddd1750901221333x5356f4f3l6b6410fc05d4e6d4@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
> #!/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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?560f92640901221451j2e2b259bw1559a8c8d8912941>