本文介绍了为什么要睡觉等待重击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在 docker-compose.yml 。 .yml中的两个相关行是:I'm having trouble understanding the startup commands for the services in this docker-compose.yml. The two relevant lines from the .yml are:command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"和entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"为什么要发送 sleep 命令到后台然后等待呢?为什么不直接睡眠6h ?另外,双美元符号是否只是在 $ {!} 中转义美元符号?Why send the sleep command to the background and then wait on it? Why not just do sleep 6h directly? Also, is the double dollar sign just escaping the dollar sign in ${!}?其他将睡眠和等待结合使用的地方,但似乎没有一个关于原因的解释:I'm finding other places where sleep and wait are used in conjunction, but none seem to have any explanation of why: http://www.masteringunixshell.net/qa17/bash-how-to-wait-seconds。 html https://stackoverflow.com/a/13301329/828584 https://superuser.com/a/753984/98583http://www.masteringunixshell.net/qa17/bash-how-to-wait-seconds.htmlhttps://stackoverflow.com/a/13301329/828584https://superuser.com/a/753984/98583推荐答案当有人想要处理时,在后台休眠然后等待是有意义的及时发出信号。It makes sense to sleep in background and then wait, when one wants to handle signals in a timely manner.(详细说明(detailed explanation here).虽然第二个示例实现了信号处理程序,但对于第一个示例,睡眠是否为是否在前台执行。没有陷阱,信号也不会传播到 nginx 进程。 要使其响应 SIGTERM 信号,入口点应该是这样的:While the second example implements a signal handler, for the first one it makes no difference whether the sleep is executed in foreground or not. There is no trap and the signal is not propagated to the nginx process.To make it respond to the SIGTERM signal, the entrypoint should be something this:/bin/sh -c 'nginx -g \"daemon off;\" & trap exit TERM; while :; do sleep 6h & wait $${!}; nginx -s reload; done'要对其进行测试:docker run --name test --rm --entrypoint="/bin/sh" nginx -c 'nginx -g "daemon off;" & trap exit TERM; while :; do sleep 20 & wait ${!}; echo running; done'停止容器docker stop test或发送 TERM 信号( docker stop 发送 TERM 后跟 KILL 如果主进程没有退出)or send the TERM signal (docker stop sends a TERM followed by KILL if the main process does not exit)docker kill --signal=SIGTERM test这样做,脚本将立即退出。现在,如果我们删除 wait $ {!} ,则在 sleep 结束时执行陷阱。By doing this, the scripts exits immediately. Now if we remove the wait ${!} the trap is executed when sleep ends. All that works well for the second example too.注意:在这两种情况下,目的都是每12小时检查一次证书更新,并每6小时重新加载配置,如指南 这两个命令就可以了。恕我直言,第一个示例中的额外等待只是对开发人员的监督。Note: in both cases the intention is to check certificate renewal every 12h and reload the configuration every 6h as mentioned in the guideThe two commands do that just fine. IMHO the additional wait in the first example is just an oversight of the developers. 已编辑:上面的合理化似乎是为了给背景睡眠背后的可能原因,可能会造成一些混乱。 (有一篇相关的文章为什么在docker后台使用nginx的守护进程关闭功能?)。It seems the rationalization above, which was meant to give possible reasons behind the background sleep, might create some confusion.(There is a related post Why use nginx with "daemon off" in background with docker?).虽然上面答案中建议的命令比该问题中的命令有所改进,但它仍然存在缺陷,因为如链接文章中所述, nginx 服务器应该是主要进程,而不是子进程。使用 exec 系统调用可以轻松实现。脚本变为:While the command suggested in the answer above is an improvement over the one in the question it is still flawed because, as mentioned in the linked post, the nginx server should be the main process and not a child. That can be easily achieved using the exec system call. The script becomes:'while :; do sleep 6h; nginx -s reload; done & exec nginx -g "daemon off;"'(部分中的更多信息配置为 Docker最佳做法)中的PID 1(More info in section Configure app as PID 1 in Docker best practices)恕我直言,这要好得多,因为不仅监视 nginx 而且还处理信号。例如,配置重载( nginx -s重载)也可以通过简单地发送 HUP 信号来手动完成。到Docker容器(请参见控制nginx )。This, IMHO, is far better because not only is nginx monitored but it also handle signals. A configuration reload (nginx -s reload), for example, can also be done manually by simply sending the HUP signal to the docker container (See Controlling nginx). 这篇关于为什么要睡觉等待重击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!