SIGCHLD in bash

SIGCHLD in bash

am 14.01.2008 21:52:46 von cts.private

I'd like to catch SIGCHLD in bash. According to the man page, it looks
like it should be possible, but the following pgm takes just about any
signal but 17: SIGCHLD. I'm running SuSE 10.3 from home. At work, I
also can't get it to work with kubuntu. Does anybody know why the man
page is wrong (seemingly).

sig=1
while [ $sig -lt 30 ]; do
trap "echo got $sig" $sig
((sig=$sig+1))
done

trap

#(sleep 2; /bin/pwd)&
#/bin/pwd

echo ${1:-"undefined"}

/bin/kill -${1:-1} $$

sleep 3

echo done

Re: SIGCHLD in bash

am 15.01.2008 02:59:56 von Barry Margolin

In article ,
Charles T Smith wrote:

> I'd like to catch SIGCHLD in bash. According to the man page, it looks
> like it should be possible, but the following pgm takes just about any
> signal but 17: SIGCHLD. I'm running SuSE 10.3 from home. At work, I
> also can't get it to work with kubuntu. Does anybody know why the man
> page is wrong (seemingly).
>
> sig=1
> while [ $sig -lt 30 ]; do
> trap "echo got $sig" $sig
> ((sig=$sig+1))
> done
>
> trap
>
> #(sleep 2; /bin/pwd)&
> #/bin/pwd
>
> echo ${1:-"undefined"}
>
> /bin/kill -${1:-1} $$
>
> sleep 3
>
> echo done

The problem is that a shell's main job is executing child processes, and
it needs to catch SIGCHLD for its own purposes. So it can't pass it on
to the script.

--
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: SIGCHLD in bash

am 16.01.2008 02:37:25 von Kaz Kylheku

On Jan 14, 5:59=A0pm, Barry Margolin wrote:
> In article ,
> =A0Charles T Smith wrote:
>
>
>
>
>
> > I'd like to catch SIGCHLD in bash. =A0According to the man page, it look=
s
> > like it should be possible, but the following pgm takes just about any
> > signal but 17: SIGCHLD. =A0I'm running SuSE 10.3 from home. =A0At work, =
I
> > also can't get it to work with kubuntu. =A0Does anybody know why the man=

> > page is wrong (seemingly).
>
> > sig=3D1
> > while [ $sig -lt 30 ]; do
> > =A0 =A0 trap "echo got $sig" $sig
> > =A0 =A0 ((sig=3D$sig+1))
> > done
>
> > trap
>
> > #(sleep 2; /bin/pwd)&
> > #/bin/pwd
>
> > echo ${1:-"undefined"}
>
> > /bin/kill -${1:-1} $$
>
> > sleep 3
>
> > echo done
>
> The problem is that a shell's main job is executing child processes, and
> it needs to catch SIGCHLD for its own purposes. =A0So it can't pass it on
> to the script.

These two uses are not mutually exclusive; Bash could trap the signal
to take care of its own actions and then chain to the script-defined
handler.

The Bash 3.1 manual page says:

The shell learns immediately whenever a job changes state.
Normally,
bash waits until it is about to print a prompt before reporting
changes
in a job=E2s status so as to not interrupt any other output.
If the -b
option to the set builtin command is enabled, bash reports such
changes
immediately. Any trap on SIGCHLD is executed for each
child that
exits.

This implies that traps on SIGCHILD are possible and executed under
the expected conditions.

Re: SIGCHLD in bash

am 16.01.2008 02:42:18 von Kaz Kylheku

On Jan 14, 12:52=A0pm, Charles T Smith wrote:
> I'd like to catch SIGCHLD in bash.

Works for me.

0:mahi:/home/kaz$ trap 'printf "\nsomething terminated!\n"' SIGCHLD
0:mahi:/home/kaz$ /bin/true

something terminated!
0:mahi:/home/kaz$ /bin/true &
[1] 7800
0:mahi:/home/kaz$
something terminated!

[1]+ Done /bin/true
0:mahi:/home/kaz$ echo $BASH_VERSION
3.1.7(1)-release

Re: SIGCHLD in bash

am 16.01.2008 19:57:44 von cts.private

On Tue, 15 Jan 2008 17:42:18 -0800, Kaz Kylheku wrote:

> On Jan 14, 12:52 pm, Charles T Smith wrote:
>> I'd like to catch SIGCHLD in bash.
>
> Works for me.
>
> 0:mahi:/home/kaz$ trap 'printf "\nsomething terminated!\n"' SIGCHLD
> 0:mahi:/home/kaz$ /bin/true
>
> something terminated!
> 0:mahi:/home/kaz$ /bin/true &
> [1] 7800
> 0:mahi:/home/kaz$
> something terminated!
>
> [1]+ Done /bin/true 0:mahi:/home/kaz$ echo
> $BASH_VERSION
> 3.1.7(1)-release


Okay somebody gave the answer in ... gnu.bash.bug, I think it was:

Shell scripts don't have job control on by default. You need to do a

set -o monitor

first. Then it works (the example presented above was not in a script,
which is where the problem occurs).