先说一下什么是 pstree
, pstree
以树结构显示进程(–display a tree of processes)
其优点在于您可以立即找出某进程的父进程:当您想要终止整个进程系列而且其中存在了许许多多的下属分支进程,您只需终止最上层的进程即可。
您可能会需要使用 -p 选项显示每个进程的 PID,以及 -u 选项来显示启动该进程的用户名。一般来说,这一树结构比较长,您就需要这么执行 pstree
:
$ pstree -up | less # 这将让您纵览整个进程树结构。
但是centos7默认并没有安装pstree,所以会有pstree:command not found
安装方式使用命令即可:
yum install psmisc
[root@CentOS7 ~]# yum install psmisc
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.nwsuaf.edu.cn
* updates: mirrors.163.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 psmisc.x86_64.0.22.20-9.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
================================================================================
Package 架构 版本 源 大小
================================================================================
正在安装:
psmisc x86_64 22.20-9.el7 base 140 k
事务概要
================================================================================
安装 1 软件包
总下载量:140 k
安装大小:479 k
Is this ok [y/d/N]: y
Downloading packages:
psmisc-22.20-9.el7.x86_64.rpm | 140 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : psmisc-22.20-9.el7.x86_64 1/1
验证中 : psmisc-22.20-9.el7.x86_64 1/1
已安装:
psmisc.x86_64 0:22.20-9.el7
完毕!
linux 优雅的退出/关闭/重启进程
通过执行如下命令,可以获取Gunicorn进程树:
pstree -ap|grep nginx
[root@CentOS7 ~]# pstree -ap|grep nginx
|-nginx,14838
| `-nginx,14839
| |-grep,14851 --color=auto nginx
[root@CentOS7 ~]#
然后:
kill -TERM 14838
杀死所有的进程:
[root@CentOS7 ~]# pstree -ap|grep nginx
| |-grep,15032 --color=auto nginx
[root@CentOS7 ~]#