Read the following: How to send a SIGINT signal from script to script? Bash
Also from info bash
To facilitate the implementation of the user interface to job control, the operating system maintains the notion of a current terminal process group ID. Members of this process group (processes whose process group ID is equal to the current terminal process group ID) receive keyboard- generated signals such as SIGINT. These processes are said to be in the foreground. Background processes are those whose process group ID differs from the terminal's; such processes are immune to keyboard-genโ erated signals.
So, bash differentiates background processes from foreground processes using the process group identifier. If the process group identifier is equal to the process identifier, then the process is the front process and terminates when it receives a SIGINT signal. Otherwise, it will not be completed (if it does not fall into the trap).
You can see the process group id with
ps x -o "%p %r %y %x %c "
Thus, when you start a background process (with & ) from a script, it will ignore the SIGINT signal if it does not fall into the trap.
However, you can kill the child process with other signals such as SIGKILL , SIGTERM , etc.
For example, if you change your script to the following, it will successfully complete the child process:
#!/bin/bash if [ "$1" = "--child" ]; then sleep 1000 elif [ "$1" = "--parent" ]; then "$0" --child & for child in $(jobs -p); do echo kill "$child" && kill "$child" done wait $(jobs -p) else echo "Must be invoked with --child or --parent." fi
Output:
$ ./test.sh --parent kill 2187 ./test.sh: line 10: 2187 Terminated "$0" --child
user000001 Feb 04 '13 at 22:36 2013-02-04 22:36
source share