Rootop 服务器运维与web架构

Linux下批量杀死进程

| 暂无评论

一台web,web访问不了,重启httpd失败,存在大量进程,只好杀掉进程再启动,可是进程比较多,不能用kill一个一个的杀吧。用到一个命令:

pkill

look up or signal processes based on name and other attributes

pkill [-signal] [-fvx] [-n|-o] [-P ppid,…] [-g pgrp,…]
[-s sid,…] [-u euid,…] [-U uid,…] [-G gid,…]
[-t term,…] [pattern]

pkill [-signal] [-fvx] [-n|-o] [-P ppid,…] [-g pgrp,…]            [-s sid,…] [-u euid,…] [-U uid,…] [-G gid,…]            [-t term,…] [pattern]

有好多参数,我这里用的是 – u 根据用户名来结束进程。先ps查看进程是用哪个用户运行的。

[root@oracle smb]# ps axu | grep httpd

root      4121  0.0  0.0 138860  2636 ?        Ss   11:10   0:00 /usr/local/apache/bin/httpd -k start

daemon    4139  0.0  0.0 138860  1944 ?        S    11:10   0:00 /usr/local/apache/bin/httpd -k start

可以看到是用daemon(还有一个root)运行,那么结束掉跟这个用户所有相关的进程

[root@oracle smb]# pkill -u daemon
另一种方法,根据关键字结束进程:
ps -ef | grep httpd | grep -v grep | cut -c 9-15 | xargs kill -9

cut -c 9-15 截取输入行的第9到第15个字符也就是是进程号。

xargs kill -9 xargs 命令是用来把前面命令的输出结果(PID)作为kill -9命令的参数

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/1320.html

作者:Venus

服务器运维与性能优化

发表回复