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?
catprocess finishes. Try your original script and pressCtrl+Dto make thecatprocess exit. While thecatprocess is in the foreground, theHUPsignal is not acted upon. Try again withcatreplaced byread(a shell built-in). – Kusalananda Aug 23 '17 at 09:59while true; do read; donein 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