Tho*_*ith 10 bash signals launchd
我有一个startd运行的脚本启动服务器,然后告诉它在launchd将其关闭时(优先应该在关闭时)正常退出.我的问题:在获得信号之前,告诉脚本空闲的适当的惯用方法是什么?我应该只使用while-true-sleep-1循环,还是有更好的方法来做到这一点?
#!/bin/bash
cd "`dirname "$0"`"
trap "./serverctl stop" TERM
./serverctl start
# wait to receive TERM signal.
Run Code Online (Sandbox Code Playgroud)
wait与自旋锁相比,普通锁的资源密集程度要低得多,即使其中包含睡眠。
您可以简单地使用“ sleep infinity”。如果要在关机时执行更多操作,并且不想为此创建函数,则可以选择以下方法:
#!/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
Run Code Online (Sandbox Code Playgroud)
例如,“ sleep infinity”的另一种替代方法(例如,似乎busybox不支持它)可以是“ tail -fn0 $ 0”。