Languages
[Edit]
EN

Bash - forward SIGTERM to child precesses

3 points
Created by:
Root-ssh
69140

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:

  • terminate function is called when SIGTERM occurs,
  • & atĀ the end of the /path/to/script_or_program_N runs script orĀ program in background, letting to get run process PID usingĀ $!,
  • kill -TERM forwardsĀ SIGTERM into child process,
  • wait blocks current script during process is run.
Native Advertising
50 000 ad impressions - 449$
šŸš€
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

ā¤ļøšŸ’» šŸ™‚

Join