The nginx. PID file is lost and logs cannot be rotated normally. Solution:
Cause of failure, after the log is migrated, The kill-USR1 PID is not successful, causing nginx to write the file handle is still in the old file.
Fault Simulation:
1: Our MV log file is. Bak
2: Clear the nginx. PID file.
3: The reload attempt fails because the PID file is empty. In this case, killall nginx is used and then nginx is started.
[[Email protected] nginx] # ps-Ef | grep nginx
Root 10287 1 0 :18? 00:00:00 nginx: Master process/usr/sbin/nginx-C/etc/nginx. conf
Nobody 10290 10287 0? 00:00:00 nginx: Worker Process
Nobody 10291 10287 0? 00:00:00 nginx: Worker Process
Nobody 10292 10287 0? 00:00:00 nginx: Worker Process
Nobody 10295 10287 0? 00:00:00 nginx: Worker Process
Nobody 10296 10287 0? 00:00:00 nginx: Worker Process
Nobody 10297 10287 0? 00:00:00 nginx: Worker Process
Nobody 10298 10287 0? 00:00:00 nginx: Worker Process
Nobody 10299 10287 0? 00:00:00 nginx: Worker Process
Nobody 10300 10287 0? 00:00:00 nginx: Worker Process
Nobody 10301 10287 0? 00:00:00 nginx: Worker Process
Nobody 10304 10287 0? 00:00:00 nginx: Worker Process
Nobody 10306 10287 0? 00:00:00 nginx: Worker Process
Nobody 10307 10287 0? 00:00:00 nginx: Worker Process
Nobody 10308 10287 0? 00:00:00 nginx: Worker Process
Nobody 10310 10287 0? 00:00:00 nginx: Worker Process
Nobody 10311 10287 0? 00:00:00 nginx: Worker Process
Root 10713 10080 0 00:00:00 pts/0 grep -- color = auto nginx
[[Email protected] nginx] # less/var/run/nginx. PID
10287
[[Email protected] nginx] # pwd
/Etc/nginx
[[Email protected] nginx] # mv/data/logs/access. log/data/logs/access. log. Bak
[[Email protected] nginx] # cd/data/logs/
[[Email protected] logs] # ls
Access. log. Bak error. Log nginx_access.log nginx_error.log nginxlog. Sh
[[Email protected] logs] # echo "">/var/run/nginx. PID
[[Email protected] logs] #/etc/init. d/nginx reload
Reloading nginx: [failed]
Smooth Solution:
1: Generate logs smoothly without restarting. We found that access. Log already exists.
2: But the PID file is still empty, so nginx reload fails.
3: overwrite/var/run/nginx. PID.
4: nginx reload is successful. This process does not involve any restart of nginx, and 0 requests are lost.
[[Email protected] logs] # ps-Ef | grep nginx | grep root | grep-V grep | awk '{print $2}' | xargs-I kill-usr1 {}
[[Email protected] logs] # ls
Access. Log Access. log. Bak error. Log nginx_access.log nginx_error.log nginxlog. Sh
[[Email protected] logs] #/etc/init. d/nginx reload
Reloading nginx: [failed]
[[Email protected] logs] # ps-Ef | grep nginx | grep root | grep-V grep | awk '{print $2}' | xargs-I echo {}>/ var/run/nginx. PID
[[Email protected] logs] #/etc/init. d/nginx reload
Reloading nginx: [OK]
Comparison of solutions:
The first method is killallnginx and then start.
1: If only one nginx server is used, requests may be lost.
2: It is possible that it cannot be started due to a hard restart.
In the second method, smooth restart will not lose any cleanup and there is no danger. You can execute it in the script.
This article is from the "Linux system maintenance" blog, please be sure to keep this source http://linuxadmin.blog.51cto.com/2683824/1564677
Nginx. PID file loss for no reason, logs cannot be rotated normally