Share a nginx restart script
Nginx is a super stable server, which usually does not need to be restarted because of overload. The purpose of restarting is to modify the configuration file and load it.
In the beginning, I used the most direct way to restart
[@ more@] killall-9 nginx;/data/nginx/sbin/nginx
If the machine is slow and the kill process cannot be killed in an instant, you can execute it again. This restart method is not particularly safe. If the configuration is incorrect, the restart will fail. You need to modify the configuration file and then start it again, which will take some time. However, for the http world, which is generally not strict at present, this time will not cause much loss, as long as it is not done at a critical moment. If you want to follow this restart method, I suggest you test it first.
And then I saw an even more wonderful restart on nginx.net.
Kill-HUP $pid ($pid is the process number of the nginx master process)
I usually use it like this.
Kill-HUP `cat / data/nginx/logs/ nginx.pid`
The advantage of this approach is to achieve a "smooth restart". As can be seen in ps-aux, the nginx starts the new process first, and the old process still provides services. After a period of time, the old process service is automatically shut down, and the new process continues to serve. But this approach also has drawbacks, if the configuration file is wrong, or resources conflict, then the restart fails, but nginx does not have any hints! This will often find that the changed configuration file does not take effect, and it is more difficult to find the problem.
So, finally mixed up the problem, got a nginx.sh, this version of nginx.sh still did not solve the kill-HUP resource conflict problem, but solved the configuration file problem. Resource conflicts, such as port 80 is occupied, the log file directory has not been created, I will think of a way.
#! / bin/sh
BASE_DIR='/data/'
${BASE_DIR} nginx/sbin/nginx-t-c ${BASE_DIR} nginx/conf/nginx.conf > & ${BASE_DIR} nginx/logs/nginx.start
Info= `cat ${BASE_DIR} nginx/logs/ nginx.start`
If [`echo $info | grep-c "syntax is ok" `- eq 1]; then
If [`ps aux | grep "nginx" | grep-c "master" `= = 1]; then
Kill-HUP `cat ${BASE_DIR} nginx/logs/ nginx.pid`
Echo "ok"
Else
Killall-9 nginx
Sleep 1
${BASE_DIR} nginx/sbin/nginx
Fi
Else
Echo "# error: #"
Cat ${BASE_DIR} nginx/logs/nginx.start
Fi