简体   繁体   English

如何重新加载nginx配置(如果正在运行)或启动(如果已停止)?

[英]How to reload nginx config if it is running or start if it's stopped?

I update nginx config files periodically and due to technical issues sometimes it can happen that nginx is not running. 我会定期更新nginx配置文件,由于技术问题,有时会发生nginx未运行的情况。

In this case if I run nginx -s reload , I see this error: 在这种情况下,如果我运行nginx -s reload ,则会看到此错误:

nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory) nginx:[错误] open()“ /run/nginx.pid”失败(2:无此类文件或目录)

How can I reload nginx config with the command from above if it's running or start if it's not running without seeing that error? 如果正在运行,如何从上方使用命令重新加载nginx config;如果没有运行该命令,则如何重新启动nginx config而不看到该错误?

If you can't count on the fact that nginx is already running then you should just restart it. 如果您不能指望nginx已经在运行,则应该重新启动它。 But, if you really want the above, assuming you're running on linux/unix and it looks like you are then you can create a tiny shell script. 但是,如果您确实需要上述内容,那么假设您正在linux / unix上运行,并且看起来像是在上面,则可以创建一个很小的shell脚本。 Again, assuming your default shell is bash or bash compatible (other shells would have to be ported) you could do something like this: 同样,假设您的默认外壳是bash或bash兼容的(必须移植其他外壳),您可以执行以下操作:

#!/bin/sh

if [ -f /run/nginx.pid ]; then
   /path/to/nginx -s reload
else
   /path/to/whatever/command/starts/nginx
fi

You can take the above to a whole new level by using inotify. 您可以使用inotify将以上内容提升到一个全新的水平。 Again, assuming you're running a Linux distribution, you can install your OS distribution's inotify-tools package and automate running the above script file every time your config files change. 同样,假设您正在运行Linux发行版,则可以在每次配置文件更改时安装OS发行版的inotify-tools软件包并自动运行上述脚本文件。 See this for more info on using inotify: 请参阅此以获取有关使用inotify的更多信息:

How to execute a command when a file changes 文件更改时如何执行命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM