I've got the following script:
#!/bin/bash
echo "We are $$"
trap "echo HUP" SIGHUP
cat # wait indefinitely
When I send SIGHUP
(using kill -HUP pid
), nothing happens.
If I change the script slightly:
#!/bin/bash
echo "We are $$"
trap "kill -- -$BASHPID" EXIT # add this
trap "echo HUP" SIGHUP
cat # wait indefinitely
...then the script does the echo HUP
thing right as it exits (when I press Ctrl+C):
roger@roger-pc:~ $ ./hupper.sh
We are 6233
^CHUP
What's going on? How should I send a signal (it doesn't necessarily have to be SIGHUP
) to this script?
cat
process finishes. Try your original script and pressCtrl+D
to make thecat
process exit. While thecat
process is in the foreground, theHUP
signal is not acted upon. Try again withcat
replaced byread
(a shell built-in). – Kusalananda Aug 23 '17 at 09:59while true; do read; done
in the end, otherwise entering text causes it to quit as well, and I want it to quit on Ctrl+C. – Roger Lipscombe Aug 23 '17 at 10:05