Bash - How should I stand until I get a signal?

I have a script to launch the launch, which starts the server, and then says that it exits gracefully when launchd kills it (which should be on shutdown). My question is: what is the appropriate, idiomatic way to tell a downtime script until it receives a signal? Should I use a while-true-sleep-1 loop or is there a better way to do this?

#!/bin/bash cd "`dirname "$0"`" trap "./serverctl stop" TERM ./serverctl start # wait to receive TERM signal. 
+6
source share
3 answers

A simple wait will be significantly less resource intensive than locking the back, even with sleep in it.

+2
source

Why do you want your script to work? Is there a reason? If you do not do anything later after the signal, then I see no reason for this.

When you get TERM from shutdown, your server and server executables (if any) also receive TERM at the same time.

To do this by design, you need to install the serverctl script as an rc script and let init (start and) stop it. Here I described how to set up a server process that was not originally designed to work as a server.

+2
source

You can just use the "infinity of sleep." If you want to perform more actions on shutdown and do not want to create a function for this, an alternative could be:

 #!/bin/bash sleep infinity & PID=$! trap "kill $PID" INT TERM echo starting # commands to start your services go here wait # commands to shutdown your services go here echo exited 

Another alternative to "infinity of sleep" (for example, busybox does not support it, for example) could be, for example, "tail -fn0 $ 0".

+1
source

Source: https://habr.com/ru/post/902817/


All Articles