In a bash script, is there a simple way to wait for a signal, something like:

wait -s SIGINT

or whatever? Maybe just trap?

  • If you could explain a bit about what your specific use case is, I could possibly give a better answer. – Kusalananda Jun 19 '18 at 6:29


wait is exclusively used in a parent process to wait for the termination of a child process (and to access its exit status).

Furthermore, no process may trap the KILL signal (the original question used KILL as example).

Also, to "wait for a signal" is an unusual thing to want to do, as signals are asynchronous events.

You could, obviously, do something like

trap 'quit=1' USR1

while [ "$quit" -ne 1 ]; do
    printf 'do "kill -USR1 %d" to exit this loop after the sleep\n' "$$"
    sleep 1

but in bash the same thing could be done with a read with a timeout:

while ! read -t 1; do
    echo 'press enter to exit loop'

or just

read -p 'press enter to continue'
  • huh? you wait for background child processes to die, and those are async events. what would be so different about waiting for an async signal? – Alexander Mills Jun 19 '18 at 6:42
  • i am def looking to do this without polling / timeout, if possible – Alexander Mills Jun 19 '18 at 6:43
  • i didnt mean just KILL by the way, I meant to include SIGTERM and SIGINT, sorry if that was confusing. I updated the question to make it better, I made a mistake by using KILL/SIGKILL instead of SIGINT, etc. – Alexander Mills Jun 19 '18 at 6:44
  • @AlexanderMills wait is a synchronization point in the program allowing the parent to wait for the child/children to finish their processing. Signals are exceptional events, occuring at any point in the program's life cycle. Again, if you could explain in your question what kind of use case you have, a better explanation could be written. – Kusalananda Jun 19 '18 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.