August Feng

Propagating linux signals

studies

trap does interrupt processes

The trap command does not interrupt processes in scripts.

Given the following script content, if we kill -s TERM <pid> the during the sleep command, it will only print after sleep exits.

trap 'printf world' SIGTERM
printf hello ; sleep 8 ; printf .

wait

This command is very feature-full and I need to write things down.

id

The id field is optional, but if given, wait will only wait for those.

gsleep infinity & gsleep infinity &
wait %1 # pgrep gsleep | head -n1 | xargs kill -s TERM

If there is no id field that is given, then wait will wait for all existing jobs.

gsleep infinity &
gsleep infinity & gsleep infinity &
wait # killall gsleep

-n

If -n flag is supplied, it only waits for one of the jobs in the supplied job list. If no jobs are supplied, it only waits for the first one.

gsleep infinity & gsleep infinity &
wait -n $(pgrep gsleep) # pgrep gsleep | head -n1 | xargs kill -s TERM

-p

This command supplements the -n flag to know which job was waited for.

gsleep infinity &
wait -p foobar -n $(pgrep gsleep) # pgrep gsleep | head -n1 | xargs kill -s TERM
echo ${foobar}

-f

The wait command is generalized to status changes as well. Supplying -f configures it to only wait for process termination.

Without the flag, wait will exit.

gsleep infinity &
wait # pgrep gsleep | kill -s STOP

With the flag, wait won't exit until the process terminates.

gsleep infinity &
wait -f %% # pgrep gsleep | xargs kill -s STOP && pgrep gsleep | xargs kill -s CONT

useful links

http://mywiki.wooledge.org/SignalTrap#When_is_the_signal_handled.3F