1

I'm trying to make a bash script wait for a signal using and empty named pipe. I like this approach most than:

while : ; do sleep 1 ; done

because it's a kind of busy-waiting

I try:

trap 'echo SIGNAL!' INT
while true;
do
  read
  echo 'AFTER READ'
done < /tmp/fifo

where /tmp/fifo is the empty named pipe

and I get:

bash: /tmp/fifo: Interrupted system call
SIGNAL!

bash script aborts. How can I make the script keep looping when receiving the signal? Thanks

francesc
  • 343
  • 3
  • 12
  • If you just want the script to pause itself, have it send itself `SIGSTOP`; later, when you are ready for it to resume, send it `SIGCONT`. This doesn't require any special handling on the part of the script itself. – chepner Dec 04 '13 at 19:12
  • Nope, since script has been stopped, it doesn't react to signals sent to him, p.e. SIGINT – francesc Dec 04 '13 at 19:19

1 Answers1

3

To expand on this answer:

trap 'echo SIGNAL!; kill $(jobs -p)' INT
read < /tmp/fifo &
wait
Community
  • 1
  • 1
chepner
  • 497,756
  • 71
  • 530
  • 681
  • ok, that works, I just didn't want to create an additional proces – francesc Dec 04 '13 at 19:31
  • This is just a single `fork`, so the overhead is fairly minimal. – chepner Dec 04 '13 at 19:41
  • +1 for not neglecting your children, which the original answer did. Unfortunately, this litters my console with ugly "Terminated signal 15" warnings. Perhaps `sh -c 'trap "exit 0" TERM; read < /tmp/fifo' &` ? Or `perl -e '$SIG{TERM}=\&exit; sleep'` ? – pilcrow Dec 04 '13 at 20:03
  • @pilcrow I think those are produced by the shell's job control feature, which is only active in an interactive shell. – chepner Dec 04 '13 at 20:25
  • ok,.. I see now the alternatives. I think we can conclude there are ways in shell scripting, but not as straightforward as in C, with pause() (or sigwaitinfo() for that matter) – francesc Dec 04 '13 at 20:57