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:
#!/bin/bash
terminate() {
kill -TERM "$child_1" 2> /dev/null
kill -TERM "$child_2" 2> /dev/null
}
trap terminate SIGTERM
/path/to/script_or_program_1 &
child_1=$!
/path/to/script_or_program_2 &
child_2=$!
wait "$child_1"
wait "$child_2"
Where:
terminatefunction is called whenSIGTERMoccurs,&at the end of the/path/to/script_or_program_Nruns script or program in background, letting to get run processPIDusing$!,kill -TERMforwardsSIGTERMinto child process,waitblocks current script during process is run.