Bash – How should I idle until I get a signal? Bash – How should I idle until I get a signal? bash bash

Bash – How should I idle until I get a signal?


You can simply use "sleep infinity". If you want to perform more actions on shutdown and don't want to create a function for that, an alternative could be:

#!/bin/bashsleep infinity & PID=$!trap "kill $PID" INT TERMecho starting# commands to start your services go herewait# commands to shutdown your services go hereecho exited

Another alternative to "sleep infinity" (it seems busybox doesn't support it for example) could be "tail -fn0 $0" for example.


A plain wait would be significantly less resource-intensive than a spin lock, even with a sleep in it.


Why would you like to keep your script running? Is there any reason? If you don't do anything later after signal then I do not see a reason for that.

When you get TERM from shutdown then your serverctl and server executable (if there is any) also gets TERM at the same time.

To do this thing by design you have to install your serverctl script as rc script and let init (start and) stop that. Here I described how to set up server process that is not originally designed to work as server.