Reputation: 10413
I have the following script, which has tail --pid=${somepid} -f ${mylogs}
. I want to catch SIGTERM
and do some graceful shutdown on that PID, because that process does not understand SIGTERM
and dies painfully.
echo "pid: $$"
trap_with_arg() {
func="$1" ; shift
for sig ; do
trap "$func $sig" "$sig"
done
}
func_trap() {
echo Trapped: $1
}
trap_with_arg func_trap INT TERM EXIT STOP
tail -f /dev/null
When I use kill -15 ${bashpid}
from another terminal, the trapped signal is not printed until I use CTRL+C. When the last command is not a process, but rather a read
which is a shell built-in, the trapped signals are printed immediately. Why is this the case? If I do the following it works:
tail -f /dev/null &
wait $!
Upvotes: 3
Views: 1342
Reputation: 136208
If you have a look at man bash
:
If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes. When bash is waiting for an asynchronous command via the
wait
builtin, the reception of a signal for which a trap has been set will cause the wait builtin to return immediately with an exit status greater than 128, immediately after which the trap is executed.
Upvotes: 8