EN
Bash - forward SIGTERM to child processes
3 points
In this short article we would like to show how to run multiple programs as child of main script and forward TERM SIGNAL
to them when occurs.
Quick solution:
xxxxxxxxxx
1
2
3
terminate() {
4
kill -TERM "$child_1" 2> /dev/null
5
kill -TERM "$child_2" 2> /dev/null
6
}
7
8
trap terminate SIGTERM
9
10
/path/to/script_or_program_1 &
11
child_1=$!
12
13
/path/to/script_or_program_2 &
14
child_2=$!
15
16
wait "$child_1"
17
wait "$child_2"
Where:
terminate
function is called whenSIGTERM
occurs,&
at the end of the/path/to/script_or_program_N
runs script or program in background, letting to get run processPID
using$!
,kill -TERM
forwardsSIGTERM
into child process,wait
blocks current script during process is run.