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

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Jan 22, 2009 at 5:58 PM, Nerius Landys <nlandys@gmail.com> wrote:
> Actually, because of the "exec" in the parent script, the line below
> it, the "killing terminator process" line, never gets reached.  So the
> terminator process that waits to kill its parent always waits the full
> 5 seconds in the background.  If I pipe the output of the parent
> script through less, it waits 5 seconds before it reaches the end of
> the output.  That is annoying.  Is there any way to solve that?
>

I see what you mean. Here's another option without exec:

#!/bin/sh -T

kill_all()
{
	echo 'killing everything'
	kill $SPID $CPID 2> /dev/null
	exit 0
}

trap kill_all SIGCHLD

./child &
CPID=$!

sleep 5 &
SPID=$!

echo "child is $CPID"
echo "sleeper is $SPID"
wait

Here I'm using the fact that the termination of any child process
causes SIGCHILD to be sent to the parent. Notice the '-T' in the first
line; this is important, because without it the 'wait' call isn't
interrupted until both children are dead. Unfortunately, this is a
non-standard option, so you'll have to check if it is supported in
your environment.

- Max



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