Bash – Trap does not catch SIGTERM in child script

bashshellsignalstimeouttrap:

I have two scripts. First one is basically wrapper that calls subscript and sets timeout.

#!/bin/bash
# wrapper_script

timeout --signal=SIGTERM 50 main_script.sh

And if execution of main_script.sh takes longer than 50 s, I am trying to catch SIGTERM

#!/bin/bash
# main_script.sh

trap "echo 'Reached time limit'; rm $log_file; exit" SIGHUP SIGINT SIGTERM

I never get message "Reached time limit" printed, child script gets killed, but it does not catch SIGTERM. Do I miss something?

Best Answer

At least in the example you provided, your main_script.sh is going to exit immediately after the trap statement. If I add while :; do :; done to the end of your main_script.sh I get the "Reached time limit"

Related Question