Linux强制杀进程命令行工具
需求, 有时候我们会有手动启动程序, 但是又在后台, 没有像服务那样有start, 和stop的程序, 这时候需要用强制杀进程方式
涉及工具, awk, sed, xargs, kill
需求一:
已知端口号:
root@corleone:/usr/local# lsof -i:8001 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python3.5 2249 root 4u IPv4 39864777 0t0 TCP *:8001 (LISTEN) python3.5 2256 root 4u IPv4 39864777 0t0 TCP *:8001 (LISTEN) python3.5 2257 root 4u IPv4 39864777 0t0 TCP *:8001 (LISTEN) python3.5 2258 root 4u IPv4 39864777 0t0 TCP *:8001 (LISTEN) python3.5 2259 root 4u IPv4 39864777 0t0 TCP *:8001 (LISTEN) python3.5 2260 root 4u IPv4 39864777 0t0 TCP *:8001 (LISTEN) python3.5 2261 root 4u IPv4 39864777 0t0 TCP *:8001 (LISTEN) python3.5 2261 root 8u IPv4 40271409 0t0 TCP corleone:8001->192.168.2.105:64060 (ESTABLISHED) python3.5 2262 root 4u IPv4 39864777 0t0 TCP *:8001 (LISTEN) python3.5 2262 root 8u IPv4 40271403 0t0 TCP corleone:8001->192.168.2.105:64059 (ESTABLISHED) python3.5 2262 root 9u IPv4 40275441 0t0 TCP corleone:8001->192.168.2.105:64058 (ESTABLISHED) python3.5 2263 root 4u IPv4 39864777 0t0 TCP *:8001 (LISTEN) python3.5 2263 root 8u IPv4 40273470 0t0 TCP corleone:8001->192.168.2.105:63903 (ESTABLISHED) python3.5 2264 root 4u IPv4 39864777 0t0 TCP *:8001 (LISTEN) python3.5 2264 root 8u IPv4 40270240 0t0 TCP corleone:8001->192.168.2.105:63834 (ESTABLISHED) python3.5 2265 root 4u IPv4 39864777 0t0 TCP *:8001 (LISTEN) python3.5 2265 root 8u IPv4 40271042 0t0 TCP corleone:8001->192.168.2.105:63902 (ESTABLISHED)
lsof -i:8001 |sed '1d'| awk '{print $2}' | xargs kill -9
需求二:
已知服务:
比如我只知道要杀掉redis,但不知道redis端口号怎么办
root@corleone:/usr/local/clion-2017.2.3/bin# netstat -anpo | grep python tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 2249/python3.5 off (0.00/0/0) tcp 0 0 0.0.0.0:8002 0.0.0.0:* LISTEN 13313/python3.5 off (0.00/0/0) tcp 0 0 192.168.2.137:46332 192.168.2.137:3306 ESTABLISHED 2260/python3.5 keepalive (2976.00/0/0) tcp 0 0 192.168.2.137:8001 192.168.2.105:64059 ESTABLISHED 2262/python3.5 off (0.00/0/0) tcp 0 0 192.168.2.137:8001 192.168.2.105:64058 ESTABLISHED 2262/python3.5 off (0.00/0/0) tcp 0 0 192.168.2.137:8001 192.168.2.105:63834 ESTABLISHED 2264/python3.5 off (0.00/0/0) tcp 0 0 192.168.2.137:46162 192.168.2.137:3306 ESTABLISHED 2263/python3.5 keepalive (2965.99/0/0) tcp 0 0 192.168.2.137:8001 192.168.2.105:63902 ESTABLISHED 2265/python3.5 off (0.00/0/0) tcp 0 0 192.168.2.137:8001 192.168.2.105:64060 ESTABLISHED 2261/python3.5 off (0.00/0/0) tcp 0 0 192.168.2.137:58598 192.168.2.137:3306 ESTABLISHED 24358/python3.5 keepalive (7198.91/0/0) tcp 0 0 192.168.2.137:53178 192.168.2.137:3306 ESTABLISHED 2262/python3.5 keepalive (4882.82/0/0) tcp 0 0 192.168.2.137:51662 192.168.2.137:3306 ESTABLISHED 2261/python3.5 keepalive (2324.11/0/0) tcp 0 0 192.168.2.137:52280 180.149.131.98:80 ESTABLISHED 24358/python3.5 off (0.00/0/0) tcp 0 0 192.168.2.137:8001 192.168.2.105:63903 ESTABLISHED 2263/python3.5 off (0.00/0/0) tcp 0 0 192.168.2.137:43886 192.168.2.137:3306 ESTABLISHED 2264/python3.5 keepalive (2061.97/0/0) tcp 0 0 192.168.2.137:52874 192.168.2.137:3306 ESTABLISHED 2265/python3.5 keepalive (4822.33/0/0) unix 3 [ ] STREAM CONNECTED 5383013 14772/python unix 3 [ ] STREAM CONNECTED 5383011 14772/python unix 3 [ ] STREAM CONNECTED 5383009 14772/python
命令:
netstat -anpo | grep python | awk -F "[ /]+" '{print $7}' | xargs kill -9
netstat -anpo | grep python | awk -F "[ /]+" '{print $7}' | xargs -i -t kill -9 {} 效果一样, 这样更方便, 问了陈总
在精确一点呢:
通过二次精确过滤 $6的数据
netstat -anpo | grep python |grep LISTEN| awk -F "[ /]+" '{print $7}'
作者:沐禹辰
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。