当前位置:首页 > 技术文章 > 操作系统 > Centos

linux系统实现批量kill进程,同时适用于MAC系统

发布时间:2017-12-10 00:29:52 作者:superl 来源:TOP技术学院 点击量:

linux系统实现批量kill进程,同时适用于MAC系统

最近在用python写东西,用到了多进程,关闭后发现还有很多进程存在。如下

superldeMacBook-Pro:~ superl$ ps -ef |grep python

  501 10267  6730   0  9:40下午 ttys000    0:00.26 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 10276 10267   0  9:40下午 ttys000    0:00.01 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 10277 10267   0  9:40下午 ttys000    0:00.01 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 10278 10267   0  9:40下午 ttys000    0:00.20 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 10279 10267   0  9:40下午 ttys000    0:00.01 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 10280 10267   0  9:40下午 ttys000    0:00.17 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 10612  6730   0  9:52下午 ttys000    0:00.20 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 10615 10612   0  9:52下午 ttys000    0:00.04 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 10696  6730   0  9:55下午 ttys000    0:00.21 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 10699 10696   0  9:55下午 ttys000    0:00.38 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 11475  6730   0 10:26下午 ttys000    0:00.23 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 11478 11475   0 10:26下午 ttys000    0:00.05 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 11521  6730   0 10:28下午 ttys000    0:00.19 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 11524 11521   0 10:28下午 ttys000    0:00.07 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 11604  6730   0 10:30下午 ttys000    0:00.20 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 11607 11604   0 10:30下午 ttys000    0:00.01 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 11610  6730   0 10:30下午 ttys000    0:00.20 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 11613 11610   0 10:30下午 ttys000    0:00.06 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python test5.py

  501 11962  6730   0 10:40下午 ttys000    0:00.22 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 11965 11962   0 10:40下午 ttys000    0:00.01 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 11966 11962   0 10:40下午 ttys000    0:00.07 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 11967 11962   0 10:40下午 ttys000    0:00.07 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 11968 11962   0 10:40下午 ttys000    0:00.07 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 11969 11962   0 10:40下午 ttys000    0:00.08 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12028  6730   0 10:42下午 ttys000    0:00.23 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12031 12028   0 10:42下午 ttys000    0:00.00 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12032 12028   0 10:42下午 ttys000    0:00.06 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12033 12028   0 10:42下午 ttys000    0:00.01 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12034 12028   0 10:42下午 ttys000    0:00.05 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12035 12028   0 10:42下午 ttys000    0:00.01 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12075  6730   0 10:44下午 ttys000    0:00.22 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12079 12075   0 10:44下午 ttys000    0:00.01 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12080 12075   0 10:44下午 ttys000    0:00.08 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12081 12075   0 10:44下午 ttys000    0:00.08 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12082 12075   0 10:44下午 ttys000    0:00.08 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

  501 12083 12075   0 10:44下午 ttys000    0:00.08 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

 

  501 12163  6730   0 10:46下午 ttys000    0:00.23 /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python btcdog.py

 

总共超过27个python相关的进程,如果一个一个的用kill -9去杀,那么想想也是一件很无脑的事情~~

那么干货来了,首先,我们可以使用 ps -ef| grep python | awk '{print $2}'  获得python相关进程的pid列表,如下:

superldeMacBook-Pro:~ superl$ ps -ef| grep python | awk '{print $2}'

10233

10238

10240

10241

10242

10245

10246

10249

12268

12271

12272

12273

12274

12275

12276

12277

12278

12279

12280

12281

12282

12283

12284

 

15766

 

好吧,我们用一条命令就去杀敌这群python 吧

 

superl$ ps -ef | grep python | grep -v grep | awk '{print $2}'|xargs kill -9   

 

执行后,再次ps -ef |grep python ,恩,整个世界都干净了~

 

说明:

 

管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入。


“ps – ef”是Red Hat 里查看所有进程的命令。这时检索出的进程将作为下一条命令“grep php-fpm”的输入。

“grep python”的输出结果是,所有含有关键字“php-fpm”的进程。

“grep -v grep”是在列出的进程中去除含有关键字“grep”的进程。

“awk ‘{print $2}’”是输出第二列的数据,而这正好是进程号PID。

“xargs kill -9”中的xargs命令是用来把前面命令的输出结果(PID)作为“kill -9”命令的参数,并执行该令。

本文链接:http://www.topjishu.net/article/70.html,如有转载,请注明出处!


关键词:linux系统,MAC系统,批量kill进程


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表