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

2
  • 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. Commented Dec 4, 2013 at 19:12
  • Nope, since script has been stopped, it doesn't react to signals sent to him, p.e. SIGINT Commented Dec 4, 2013 at 19:19

1 Answer 1

3

To expand on this answer:

trap 'echo SIGNAL!; kill $(jobs -p)' INT
read < /tmp/fifo &
wait
Sign up to request clarification or add additional context in comments.

5 Comments

ok, that works, I just didn't want to create an additional proces
This is just a single fork, so the overhead is fairly minimal.
+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 I think those are produced by the shell's job control feature, which is only active in an interactive shell.
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.