Killing piped processes along with parent?

Killing piped processes along with parent?

am 17.10.2007 02:44:03 von rayvd

I have a script "script.sh" (being run by bash), which essentially is
the following:

#!/bin/bash

tail -f log | awk '
{
print $0
}
'

When I send a SIGTERM (kill pid) to script.sh, script.sh dies, but the
tail process lives on.

What's the proper way to have all this cleaned up for me
automatically? I guess I could use trap to make the script somehow
find and kill tail when it receives a TERM signal, but I figure there
must be a better way.. :-)

Thanks.

Re: Killing piped processes along with parent?

am 17.10.2007 07:03:48 von Barry Margolin

In article <1192581843.110613.131910@e9g2000prf.googlegroups.com>,
rayvd wrote:

> I have a script "script.sh" (being run by bash), which essentially is
> the following:
>
> #!/bin/bash
>
> tail -f log | awk '
> {
> print $0
> }
> '
>
> When I send a SIGTERM (kill pid) to script.sh, script.sh dies, but the
> tail process lives on.
>
> What's the proper way to have all this cleaned up for me
> automatically? I guess I could use trap to make the script somehow
> find and kill tail when it receives a TERM signal, but I figure there
> must be a better way.. :-)

All the processes are probably in a process group, whose PGID is the PID
of the script.sh process. Try killing the whole process group by
specifying the negative of this PID.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Re: Killing piped processes along with parent?

am 18.10.2007 06:03:05 von Vakayil Thobias

rayvd wrote:
> I have a script "script.sh" (being run by bash), which essentially is
> the following:
>
> #!/bin/bash
>
> tail -f log | awk '
> {
> print $0
> }
> '
>
> When I send a SIGTERM (kill pid) to script.sh, script.sh dies, but the
> tail process lives on.
>
> What's the proper way to have all this cleaned up for me
> automatically? I guess I could use trap to make the script somehow
> find and kill tail when it receives a TERM signal, but I figure there
> must be a better way.. :-)
>
> Thanks.
>

In the script, exclude the pid of the script before killing the processs.