kill命令
命令 | 作用 |
---|---|
TERM/INT | 立即关闭整个服务(不管子进程是否在提供服务) |
QUIT | “优雅的”关闭服务(等待子进程结束服务后关闭Nginx) |
HUP | 重新读配置文件使用服务对新配置项生效 |
USR1 | 重新打开日志文件,可以用来切割日志 |
USR2 | 平滑升级到最新版Nginx |
WINCH | 所有的子进程不再接收处理新的连接,相当于给work发送quit,但是不会退出Nginx |
命令演示
查看nginx状态
[root@localhost ~]# ps -ef |grep nginx
root 23372 1 0 8月04 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 23373 23372 0 8月04 ? 00:00:00 nginx: worker process
root 25956 25937 0 20:30 pts/0 00:00:00 grep --color=auto nginx
使用TERM/INT关闭服务
[root@localhost ~]# kill -INT 23372
[root@localhost ~]# ps -ef |grep nginx
root 25967 25937 0 20:43 pts/0 00:00:00 grep --color=auto nginx
使用QUIT关闭服务
[root@localhost ~]# ps -ef |grep nginx
root 25971 1 0 20:44 ? 00:00:00 nginx: master process nginx
nginx 25972 25971 0 20:44 ? 00:00:00 nginx: worker process
root 25977 25937 0 20:44 pts/0 00:00:00 grep --color=auto nginx
[root@localhost ~]# kill -QUIT 25971
[root@localhost ~]# ps -ef |grep nginx
root 25979 25937 0 20:44 pts/0 00:00:00 grep --color=auto nginx
使用HUP使配置文件重新生效 --- 注意worker进程的变化
[root@localhost ~]# ps -ef |grep nginx
root 25981 1 0 20:45 ? 00:00:00 nginx: master process nginx
nginx 25982 25981 0 20:45 ? 00:00:00 nginx: worker process
root 25984 25937 0 20:45 pts/0 00:00:00 grep --color=auto nginx
[root@localhost ~]# kill -HUP 25981
[root@localhost ~]# ps -ef |grep nginx
root 25981 1 0 20:45 ? 00:00:00 nginx: master process nginx
nginx 25985 25981 0 20:45 ? 00:00:00 nginx: worker process
root 25987 25937 0 20:45 pts/0 00:00:00 grep --color=auto nginx
使用WINCH关闭所有的子进程
[root@localhost ~]# ps -ef |grep nginx
root 25981 1 0 20:45 ? 00:00:00 nginx: master process nginx
nginx 25985 25981 0 20:45 ? 00:00:00 nginx: worker process
root 25987 25937 0 20:45 pts/0 00:00:00 grep --color=auto nginx
[root@localhost ~]# kill -WINCH 25981
[root@localhost ~]# ps -ef |grep nginx
root 25981 1 0 20:45 ? 00:00:00 nginx: master process nginx
root 25989 25937 0 20:46 pts/0 00:00:00 grep --color=auto nginx
使用USR1重新生成日志
[root@localhost nginx]# ls
access.log access.log-20210802.gz access.log-20210805 error.log-20210728.gz error.log-20210804.gz
access.log-20210728.gz access.log-20210804.gz error.log error.log-20210802.gz error.log-20210805
[root@localhost nginx]# rm -rf error.log
[root@localhost nginx]# ls
access.log access.log-20210802.gz access.log-20210805 error.log-20210802.gz error.log-20210805
access.log-20210728.gz access.log-20210804.gz error.log-20210728.gz error.log-20210804.gz
[root@localhost nginx]# ps -ef |grep nginx
root 25981 1 0 20:45 ? 00:00:00 nginx: master process nginx
root 26036 25937 0 20:48 pts/0 00:00:00 grep --color=auto nginx
[root@localhost nginx]# kill -USR1 25981
[root@localhost nginx]# ls
access.log access.log-20210802.gz access.log-20210805 error.log-20210728.gz error.log-20210804.gz
access.log-20210728.gz access.log-20210804.gz error.log error.log-20210802.gz error.log-20210805
[root@localhost nginx]# ll
总用量 52
-rw-r----- 1 nginx adm 0 8月 5 03:17 access.log
-rw-r----- 1 nginx adm 231 7月 27 16:47 access.log-20210728.gz
-rw-r----- 1 nginx adm 508 8月 1 17:18 access.log-20210802.gz
-rw-r----- 1 nginx adm 5012 8月 3 22:27 access.log-20210804.gz
-rw-r----- 1 nginx adm 8163 8月 4 22:02 access.log-20210805
-rw-r--r-- 1 nginx root 0 8月 9 20:48 error.log
-rw-r----- 1 nginx adm 451 7月 28 03:20 error.log-20210728.gz
-rw-r----- 1 nginx adm 2379 8月 2 03:32 error.log-20210802.gz
-rw-r----- 1 nginx adm 7091 8月 4 03:28 error.log-20210804.gz
-rw-r----- 1 nginx adm 12117 8月 5 03:17 error.log-20210805
USR2平滑升级
前面提到过,使用nginx进行平滑升级。
请看下图,在原本只有一个nginxmaster进程的情况下,我们使用 kill -USR2
cat /var/run/nginx.pid
—>我的PID地址
来创建一个新的nginx master进程
同事我们观察PID目录下,如果多出来一份名叫 nginx.pid.oldbin(这个是老nginx的pid)的文件,则我们可以确认第二个nginx已经打开
此时再升级,升级完成之后通过kill -QUIT nginx.pid 来关闭老的nginx实现平滑升级
我版本是最新的并没有成功,使用-USR2 不会生成新进程,为什么?
Nginx的命令行控制模式
[root@localhost nginx]# nginx -h
nginx version: nginx/1.21.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
stop --- INT / TERM
quit --- QUIT
reopen --- USR1
reload --- HUP
-p prefix : set prefix path (default: /etc/nginx/) 启动路径
-e filename : set error log file (default: /var/log/nginx/error.log) errorlog地址
-c filename : set configuration file (default: /etc/nginx/nginx.conf) 修改默认的配置文件
-g directives : set global directives out of configuration file 用来补充Nginx配置文件,向Nginx服务指定启动时应用全局的配置