I have a very simple script to trap signals:
#!/bin/bash
unlink SigTermRecieved
unlink SigIntRecieved
#trap " touch SigTermRecieved ; exit " SIGTERM
trap " touch SigIntRecieved ; exit " SIGINT
echo "pid is $$"
while true
do
sleep 60m
done
exit
BEHAVIOR
with both trap lines commented:
SIGTERM sent: terminates program.
? SIGINT sent from another terminal window is IGNORED!
CTRL C terminates program.
with SIGTERM trap uncommented:
? SIGTERM sent: program does not terminate and SIGTERM trap is not executed.
? SIGINT sent from another terminal window is IGNORED!
CTRL C terminates program.
with SIGINT trap uncommented:
SIGTERM sent: terminates program.
? SIGINT from another terminal window is IGNORED!
CTRL C executes trap code and terminates program.
The behaviors preceded by ? are unexpected or wrong. I'm not too concerned about the SIGINT behaviors. I tried those to see if any traps would work.
So, I removed the snooze function and just have:
sleep 60m &
wait $!
Now, kill -TERM -pid
kills both processes and executes the trap for SIGTERM.
In my application, I'm waiting for SIGTERM from system shutdown, so I can clean up some details. I don't know whether system shutdown uses pid
or -pid
, both work for me, and the system will eventually kill all the processes. A few dangling sleep processes while I'm testing is no big deal.
That fifo solution is quite complicated and I don't understand it.
Thanks for the help.
bash -c 'echo $$; mkfifo fifo; exec 3<>fifo; while :; do read -t2 <fifo; echo next; done'
. You may also get some other ideas (not all great) here – Jan 15 '20 at 20:42