需求, 有時候咱們會有手動啓動程序, 可是又在後臺, 沒有像服務那樣有start, 和stop的程序, 這時候須要用強制殺進程方式python
涉及工具, awk, sed, xargs, killredis
已知端口號:tcp
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端口號怎麼辦spa
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
命令:unix
netstat -anpo | grep python | awk -F "[ /]+" '{print $7}' | xargs kill -9
netstat -anpo | grep python | awk -F "[ /]+" '{print $7}' | xargs -i -t kill -9 {} 效果同樣, 這樣更方便, 問了陳總
在精確一點呢:code
經過二次精確過濾 $6的數據blog
netstat -anpo | grep python |grep LISTEN| awk -F "[ /]+" '{print $7}'