kill命令

命令 作用
TERM/INT 立即关闭整个服务(不管子进程是否在提供服务)
QUIT “优雅的”关闭服务(等待子进程结束服务后关闭Nginx)
HUP 重新读配置文件使用服务对新配置项生效
USR1 重新打开日志文件,可以用来切割日志
USR2 平滑升级到最新版Nginx
WINCH 所有的子进程不再接收处理新的连接,相当于给work发送quit,但是不会退出Nginx

命令演示

  1. 查看nginx状态
  2. [root@localhost ~]# ps -ef |grep nginx
  3. root 23372 1 0 804 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
  4. nginx 23373 23372 0 804 ? 00:00:00 nginx: worker process
  5. root 25956 25937 0 20:30 pts/0 00:00:00 grep --color=auto nginx
  6. 使用TERM/INT关闭服务
  7. [root@localhost ~]# kill -INT 23372
  8. [root@localhost ~]# ps -ef |grep nginx
  9. root 25967 25937 0 20:43 pts/0 00:00:00 grep --color=auto nginx
  10. 使用QUIT关闭服务
  11. [root@localhost ~]# ps -ef |grep nginx
  12. root 25971 1 0 20:44 ? 00:00:00 nginx: master process nginx
  13. nginx 25972 25971 0 20:44 ? 00:00:00 nginx: worker process
  14. root 25977 25937 0 20:44 pts/0 00:00:00 grep --color=auto nginx
  15. [root@localhost ~]# kill -QUIT 25971
  16. [root@localhost ~]# ps -ef |grep nginx
  17. root 25979 25937 0 20:44 pts/0 00:00:00 grep --color=auto nginx
  18. 使用HUP使配置文件重新生效 --- 注意worker进程的变化
  19. [root@localhost ~]# ps -ef |grep nginx
  20. root 25981 1 0 20:45 ? 00:00:00 nginx: master process nginx
  21. nginx 25982 25981 0 20:45 ? 00:00:00 nginx: worker process
  22. root 25984 25937 0 20:45 pts/0 00:00:00 grep --color=auto nginx
  23. [root@localhost ~]# kill -HUP 25981
  24. [root@localhost ~]# ps -ef |grep nginx
  25. root 25981 1 0 20:45 ? 00:00:00 nginx: master process nginx
  26. nginx 25985 25981 0 20:45 ? 00:00:00 nginx: worker process
  27. root 25987 25937 0 20:45 pts/0 00:00:00 grep --color=auto nginx
  28. 使用WINCH关闭所有的子进程
  29. [root@localhost ~]# ps -ef |grep nginx
  30. root 25981 1 0 20:45 ? 00:00:00 nginx: master process nginx
  31. nginx 25985 25981 0 20:45 ? 00:00:00 nginx: worker process
  32. root 25987 25937 0 20:45 pts/0 00:00:00 grep --color=auto nginx
  33. [root@localhost ~]# kill -WINCH 25981
  34. [root@localhost ~]# ps -ef |grep nginx
  35. root 25981 1 0 20:45 ? 00:00:00 nginx: master process nginx
  36. root 25989 25937 0 20:46 pts/0 00:00:00 grep --color=auto nginx
  37. 使用USR1重新生成日志
  38. [root@localhost nginx]# ls
  39. access.log access.log-20210802.gz access.log-20210805 error.log-20210728.gz error.log-20210804.gz
  40. access.log-20210728.gz access.log-20210804.gz error.log error.log-20210802.gz error.log-20210805
  41. [root@localhost nginx]# rm -rf error.log
  42. [root@localhost nginx]# ls
  43. access.log access.log-20210802.gz access.log-20210805 error.log-20210802.gz error.log-20210805
  44. access.log-20210728.gz access.log-20210804.gz error.log-20210728.gz error.log-20210804.gz
  45. [root@localhost nginx]# ps -ef |grep nginx
  46. root 25981 1 0 20:45 ? 00:00:00 nginx: master process nginx
  47. root 26036 25937 0 20:48 pts/0 00:00:00 grep --color=auto nginx
  48. [root@localhost nginx]# kill -USR1 25981
  49. [root@localhost nginx]# ls
  50. access.log access.log-20210802.gz access.log-20210805 error.log-20210728.gz error.log-20210804.gz
  51. access.log-20210728.gz access.log-20210804.gz error.log error.log-20210802.gz error.log-20210805
  52. [root@localhost nginx]# ll
  53. 总用量 52
  54. -rw-r----- 1 nginx adm 0 8 5 03:17 access.log
  55. -rw-r----- 1 nginx adm 231 7 27 16:47 access.log-20210728.gz
  56. -rw-r----- 1 nginx adm 508 8 1 17:18 access.log-20210802.gz
  57. -rw-r----- 1 nginx adm 5012 8 3 22:27 access.log-20210804.gz
  58. -rw-r----- 1 nginx adm 8163 8 4 22:02 access.log-20210805
  59. -rw-r--r-- 1 nginx root 0 8 9 20:48 error.log
  60. -rw-r----- 1 nginx adm 451 7 28 03:20 error.log-20210728.gz
  61. -rw-r----- 1 nginx adm 2379 8 2 03:32 error.log-20210802.gz
  62. -rw-r----- 1 nginx adm 7091 8 4 03:28 error.log-20210804.gz
  63. -rw-r----- 1 nginx adm 12117 8 5 03:17 error.log-20210805

USR2平滑升级

前面提到过,使用nginx进行平滑升级。
请看下图,在原本只有一个nginxmaster进程的情况下,我们使用 kill -USR2cat /var/run/nginx.pid —>我的PID地址
来创建一个新的nginx master进程
同事我们观察PID目录下,如果多出来一份名叫 nginx.pid.oldbin(这个是老nginx的pid)的文件,则我们可以确认第二个nginx已经打开
此时再升级,升级完成之后通过kill -QUIT nginx.pid 来关闭老的nginx实现平滑升级
image.png
image.png
image.png
image.png
我版本是最新的并没有成功,使用-USR2 不会生成新进程,为什么?

Nginx的命令行控制模式

  1. [root@localhost nginx]# nginx -h
  2. nginx version: nginx/1.21.1
  3. Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
  4. [-e filename] [-c filename] [-g directives]
  5. Options:
  6. -?,-h : this help
  7. -v : show version and exit
  8. -V : show version and configure options then exit
  9. -t : test configuration and exit
  10. -T : test configuration, dump it and exit
  11. -q : suppress non-error messages during configuration testing
  12. -s signal : send signal to a master process: stop, quit, reopen, reload
  13. stop --- INT / TERM
  14. quit --- QUIT
  15. reopen --- USR1
  16. reload --- HUP
  17. -p prefix : set prefix path (default: /etc/nginx/) 启动路径
  18. -e filename : set error log file (default: /var/log/nginx/error.log) errorlog地址
  19. -c filename : set configuration file (default: /etc/nginx/nginx.conf) 修改默认的配置文件
  20. -g directives : set global directives out of configuration file 用来补充Nginx配置文件,向Nginx服务指定启动时应用全局的配置