Unable to trap SIGINT signal in a background shell
Asked Answered
O

2

16

I am unable to trap a signal when running in a child / background process.

Here is my simple bash script:

#!/bin/bash

echo "in child"

trap "got_signal" SIGINT

function got_signal {
  echo "trapped"
  exit 0
}

while [ true ]; do
    sleep 2
done

When running this and later do

kill -SIGINT (pid)

everything works as expected, it prints trapped and exits.

Now, if I start the same script from a parent script like this:

#!/bin/bash

echo "starting the child"

./child.sh &

Then the child does not trap the signal anymore.... ?

After changing to use SIGTERM instead of SIGINT, it seems to be working correctly... ?

Obscurity answered 13/4, 2011 at 0:26 Comment(0)
E
15

The bash manpage on OSX (but it should be the same in other versions) has this to say about signal handling:

Non-builtin commands run by bash have signal handlers set to the values inherited by the shell from its parent. When job control is not in effect, asynchronous commands ignore SIGINT and SIGQUIT in addition to these inherited handlers.

and further on, under the trap command:

Signals ignored upon entry to the shell cannot be trapped or reset.

Since scripts don't use job control by default, this means the case you're talking about.

Entropy answered 13/4, 2011 at 0:42 Comment(0)
P
5

Per your note:

Signals ignored upon entry to the shell cannot be trapped or reset.

I have noticed that ZSH does not ignore the signals sent back and forth between parent and child process, but bash does. Here's the question I posted myself:

Trapping CHLD signal - ZSH works but ksh/bash/sh don't?

Philippa answered 5/3, 2012 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.