python 杀死子进程_python 杀死subprocess.Popen下打开的所有子进程

在Linux环境下,使用Python的subprocess.Popen开启服务后,发现Popen.kill()只能杀死终端进程,无法结束由其启动的所有子进程。通过设置os.setsid创建进程组,并在结束时使用os.killpg()方法,可以成功杀死整个进程组,实现服务的完整关闭。这种方法对于需要控制服务生命周期的场景非常有用。
摘要由CSDN通过智能技术生成

写在之前:

先来明确以下几点:

1、subprocess.Popen:这个类常用来执行系统命令的,就类似你打开一个终端窗口,然后输入命令执行的过程(具体的用法,请百度查询)

2、进程组、父进程、子进程(linux):

{

进程组pid = 父进程pid,即 父进程: 组长进程

组长进程

组长进程标识: 其进程组ID==其父进程ID

组长进程可以创建一个进程组,创建该进程组中的进程,然后终止

只要进程组中有一个进程存在,进程组就存在,与组长进程是否终止无关

进程组生存期: 进程组创建到最后一个进程离开(终止或转移到另一个进程组)

}

3、脚本的执行,是放在linux服务器上

4、我用python语言,篇幅较长,带上耐心看

一、为什么写这往篇文章

正巧遇到这样一个问题:

在linux有一个服务,在需要用的时候,再开启;

但开启后自己又不会自动结束,还需要手动去停止;

一切的执行操作均需要通过代码去完成;

开启服务很简单,直接用Popen就可以,但结束的时候,发现用Popen.kill()方法,仅仅杀死了终端进程,并不能全完杀死由 终端 所开启的所有子进程;

然后百度了一番找到了一个可行方案,记录下来方便查看和后人学习。

二、以开启结束STF服务为例

开启STF服务脚本如下(代码仅为举例,以简单为主):

import os

import time

import subprocess

from subprocess import PIPE

def start_stf():

cmd = "stf local --public-ip 192.168.2.233 --allow-remote".split()

stf_p = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE, preexec_fn=os.setsid)

# os.setsid 用于为当前进程创建进程组

time.sleep(5) # 等上几秒,开服务需求时间

print("状态:", stf_p.poll())

print("开启进程的pid", stf_p.pid)

print("所属进程组的pid", os.getpgid(stf_p.pid))

time.sleep(60) # 再等60秒,然后通过以下方式关闭服务试下

stf_p.kill()

if __name__ == "__main__":

start_stf()

测试内容:

通过stf_p.kill()是否可以杀死所有开启的子进程

在执行脚本之前查看关于STF的进程、端口情况:

端口:

[root@localhost ~]# netstat -tulnp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1070/sshd

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1282/master

tcp6 0 0 :::22 :::* LISTEN 1070/sshd

tcp6 0 0 ::1:25 :::* LISTEN 1282/master

udp 0 0 127.0.0.1:323 0.0.0.0:* 728/chronyd

udp6 0 0 ::1:323 :::* 728/chronyd

进程:

[root@localhost ~]# ps -aux | grep stf

root 1282 0.0 0.0 89544 2080 ? Ss 12:28 0:00 /usr/libexec/postfix/master -w

postfix 1283 0.0 0.0 89648 3992 ? S 12:28 0:00 pickup -l -t unix -u

postfix 1284 0.0 0.0 89716 4016 ? S 12:28 0:00 qmgr -l -t unix -u

root 1374 0.0 0.0 112676 980 pts/0 S+ 12:34 0:00 grep --color=auto stf

[root@localhost ~]#

可以看出,没有执行脚本之前,没有关于STF任务进程和端口,下面是执行脚本之后的进程情况

脚本的打印结果如下:

命令执行开启STF服务成功,开启的进程和进程组pid均为1507

# -----------------------------------------------

(venv) [root@localhost test_one]# python test_kill_popen.py

状态: None

开启进程的pid 1507

所属进程组的pid 1507

(venv) [root@localhost test_one]#

# -----------------------------------------------

在60秒结束之前,1507进程及STF相关进程和端口情况如下:

# -----------------

1507进程:

可以看出1507进程作用是执行那条命令,相当于一个终端口窗口占用的进程

[root@localhost ~]# ps -aux | grep 1507

root 1507 1.8 0.1 883816 26976 ? Ssl 12:50 0:00 node /usr/local/bin/stf local --public-ip 192.168.2.233 --allow-remote

root 1630 0.0 0.0 112680 976 pts/1 S+ 12:51 0:00 grep --color=auto 1507

# -----------------

关于STF进程及端口情况如下:

可以看出,STF服务的开启,启动了很多的子进程和端口

[root@localhost ~]# ps -aux | grep stf

root 1282 0.0 0.0 89544 2080 ? Ss 12:28 0:00 /usr/libexec/postfix/master -w

postfix 1283 0.0 0.0 89648 3992 ? S 12:28 0:00 pickup -l -t unix -u

postfix 1284 0.0 0.0 89716 4016 ? S 12:28 0:00 qmgr -l -t unix -u

root 1408 0.0 0.0 112676 980 pts/1 S+ 12:50 0:00 grep --color=auto stf

[root@localhost ~]# ps -aux | grep stf

root 1282 0.0 0.0 89544 2080 ? Ss 12:28 0:00 /usr/libexec/postfix/master -w

postfix 1283 0.0 0.0 89648 3992 ? S 12:28 0:00 pickup -l -t unix -u

postfix 1284 0.0 0.0 89716 4016 ? S 12:28 0:00 qmgr -l -t unix -u

root 1507 4.0 0.1 883816 26976 ? Ssl 12:50 0:00 node /usr/local/bin/stf local --public-ip 192.168.2.233 --allow-remote

root 1519 1.3 0.1 972320 29188 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli triproxy app001 --bind-pub tcp://127.0.0.1:7111 --bind-dealer tcp://127.0.0.1:7112 --bind-pull tcp://127.0.0.1:7113

root 1520 1.3 0.1 972320 28904 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli triproxy dev001 --bind-pub tcp://127.0.0.1:7114 --bind-dealer tcp://127.0.0.1:7115 --bind-pull tcp://127.0.0.1:7116

root 1525 3.1 0.2 977948 41660 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli processor proc001 --connect-app-dealer tcp://127.0.0.1:7112 --connect-dev-dealer tcp://127.0.0.1:7115

root 1530 3.1 0.2 979020 40020 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli processor proc002 --connect-app-dealer tcp://127.0.0.1:7112 --connect-dev-dealer tcp://127.0.0.1:7115

root 1535 3.2 0.2 979020 40116 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli reaper reaper001 --connect-push tcp://127.0.0.1:7116 --connect-sub tcp://127.0.0.1:7111

root 1544 5.2 0.2 988540 43048 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli provider --name localhost --min-port 7400 --max-port 7700 --connect-sub tcp://127.0.0.1:7114 --connect-push tcp://127.0.0.1:7116 --group-timeout 900 --public-ip 192.168.2.233 --storage-url http://localhost:7100/ --adb-host 127.0.0.1 --adb-port 5037 --vnc-initial-size 600x800 --allow-remote

root 1550 5.2 0.2 900316 40428 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli auth-mock --port 7120 --secret kute kittykat --app-url http://192.168.2.233:7100/

root 1556 7.2 0.3 906508 49644 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli app --port 7105 --secret kute kittykat --auth-url http://192.168.2.233:7100/auth/mock/ --websocket-url http://192.168.2.233:7110/

root 1562 12.7 0.4 1051004 71560 ? Sl 12:50 0:01 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli api --port 7106 --secret kute kittykat --connect-push tcp://127.0.0.1:7113 --connect-sub tcp://127.0.0.1:7111

root 1568 8.1 0.3 996096 54188 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli websocket --port 7110 --secret kute kittykat --storage-url http://localhost:7100/ --connect-sub tcp://127.0.0.1:7111 --connect-push tcp://127.0.0.1:7113

root 1574 5.1 0.2 902392 46068 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli storage-temp --port 7102

root 1580 5.7 0.2 899788 40948 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli storage-plugin-image --port 7103 --storage-url http://localhost:7100/

root 1586 5.2 0.2 900312 42636 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli storage-plugin-apk --port 7104 --storage-url http://localhost:7100/

root 1591 2.9 0.2 886632 34844 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli poorxy --port 7100 --app-url http://localhost:7105/ --auth-url http://localhost:7120/ --api-url http://localhost:7106/ --websocket-url http://localhost:7110/ --storage-url http://localhost:7102/ --storage-plugin-image-url http://localhost:7103/ --storage-plugin-apk-url http://localhost:7104/

root 1628 0.0 0.0 112680 980 pts/1 S+ 12:50 0:00 grep --color=auto stf

[root@localhost ~]# netstat -tulnp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 127.0.0.1:7115 0.0.0.0:* LISTEN 1520/node

tcp 0 0 127.0.0.1:7116 0.0.0.0:* LISTEN 1520/node

tcp 0 0 127.0.0.1:5037 0.0.0.0:* LISTEN 1616/adb

tcp 0 0 127.0.0.1:28015 0.0.0.0:* LISTEN 1430/rethinkdb

tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 1430/rethinkdb

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1070/sshd

tcp 0 0 127.0.0.1:29015 0.0.0.0:* LISTEN 1430/rethinkdb

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1282/master

tcp 0 0 127.0.0.1:7111 0.0.0.0:* LISTEN 1519/node

tcp 0 0 127.0.0.1:7112 0.0.0.0:* LISTEN 1519/node

tcp 0 0 127.0.0.1:7113 0.0.0.0:* LISTEN 1519/node

tcp 0 0 127.0.0.1:7114 0.0.0.0:* LISTEN 1520/node

tcp6 0 0 ::1:28015 :::* LISTEN 1430/rethinkdb

tcp6 0 0 :::7120 :::* LISTEN 1550/node

tcp6 0 0 ::1:8080 :::* LISTEN 1430/rethinkdb

tcp6 0 0 :::22 :::* LISTEN 1070/sshd

tcp6 0 0 ::1:29015 :::* LISTEN 1430/rethinkdb

tcp6 0 0 ::1:25 :::* LISTEN 1282/master

tcp6 0 0 :::7100 :::* LISTEN 1591/node

tcp6 0 0 :::7102 :::* LISTEN 1574/node

tcp6 0 0 :::7103 :::* LISTEN 1580/node

tcp6 0 0 :::7104 :::* LISTEN 1586/node

tcp6 0 0 :::7105 :::* LISTEN 1556/node

tcp6 0 0 :::7106 :::* LISTEN 1562/node

tcp6 0 0 :::7110 :::* LISTEN 1568/node

udp 0 0 127.0.0.1:323 0.0.0.0:* 728/chronyd

udp6 0 0 ::1:323 :::* 728/chronyd

# -----------------------------------------------

在60结束之后,即脚本运行完之后,查看stf_p.kill()的效果如何

1507端口情况:

可以看出1507端口的进程已经被杀死了

[root@localhost ~]# ps -aux | grep 1507

root 1633 0.0 0.0 112676 980 pts/1 S+ 12:51 0:00 grep --color=auto 1507

# -----------------------------------------------

stf相关进程及端口情况:

可以看出,执行stf_p.kill()并没有杀死STF的服务相关进程,仅仅只是把1507端口进程杀死了

也就是说,仅仅杀死了终端,但由终端启动的服务进程并没有杀死

# -----------------

stf相关进程:

[root@localhost ~]# ps -aux | grep stf

root 1282 0.0 0.0 89544 2080 ? Ss 12:28 0:00 /usr/libexec/postfix/master -w

postfix 1283 0.0 0.0 89648 3992 ? S 12:28 0:00 pickup -l -t unix -u

postfix 1284 0.0 0.0 89716 4016 ? S 12:28 0:00 qmgr -l -t unix -u

root 1519 0.1 0.1 972320 29188 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli triproxy app001 --bind-pub tcp://127.0.0.1:7111 --bind-dealer tcp://127.0.0.1:7112 --bind-pull tcp://127.0.0.1:7113

root 1520 0.1 0.1 972320 28904 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli triproxy dev001 --bind-pub tcp://127.0.0.1:7114 --bind-dealer tcp://127.0.0.1:7115 --bind-pull tcp://127.0.0.1:7116

root 1525 0.4 0.2 977948 41660 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli processor proc001 --connect-app-dealer tcp://127.0.0.1:7112 --connect-dev-dealer tcp://127.0.0.1:7115

root 1530 0.4 0.2 979020 40028 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli processor proc002 --connect-app-dealer tcp://127.0.0.1:7112 --connect-dev-dealer tcp://127.0.0.1:7115

root 1535 0.4 0.2 979020 40116 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli reaper reaper001 --connect-push tcp://127.0.0.1:7116 --connect-sub tcp://127.0.0.1:7111

root 1544 0.6 0.2 988540 34592 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli provider --name localhost --min-port 7400 --max-port 7700 --connect-sub tcp://127.0.0.1:7114 --connect-push tcp://127.0.0.1:7116 --group-timeout 900 --public-ip 192.168.2.233 --storage-url http://localhost:7100/ --adb-host 127.0.0.1 --adb-port 5037 --vnc-initial-size 600x800 --allow-remote

root 1550 0.7 0.2 900316 32796 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli auth-mock --port 7120 --secret kute kittykat --app-url http://192.168.2.233:7100/

root 1556 0.9 0.2 908556 41168 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli app --port 7105 --secret kute kittykat --auth-url http://192.168.2.233:7100/auth/mock/ --websocket-url http://192.168.2.233:7110/

root 1562 1.7 0.3 1052540 54724 ? Sl 12:50 0:01 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli api --port 7106 --secret kute kittykat --connect-push tcp://127.0.0.1:7113 --connect-sub tcp://127.0.0.1:7111

root 1568 1.0 0.2 1015040 43332 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli websocket --port 7110 --secret kute kittykat --storage-url http://localhost:7100/ --connect-sub tcp://127.0.0.1:7111 --connect-push tcp://127.0.0.1:7113

root 1574 0.7 0.2 904440 35524 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli storage-temp --port 7102

root 1580 0.7 0.1 900812 32244 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli storage-plugin-image --port 7103 --storage-url http://localhost:7100/

root 1586 0.7 0.2 901336 32868 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli storage-plugin-apk --port 7104 --storage-url http://localhost:7100/

root 1591 0.3 0.2 886632 34844 ? Sl 12:50 0:00 /opt/node-v8.9.0-linux-x64/bin/node /opt/node-v8.9.0-linux-x64/lib/node_modules/stf/lib/cli poorxy --port 7100 --app-url http://localhost:7105/ --auth-url http://localhost:7120/ --api-url http://localhost:7106/ --websocket-url http://localhost:7110/ --storage-url http://localhost:7102/ --storage-plugin-image-url http://localhost:7103/ --storage-plugin-apk-url http://localhost:7104/

root 1636 0.0 0.0 112680 980 pts/1 S+ 12:52 0:00 grep --color=auto stf

# -----------------

端口情况:

[root@localhost ~]# netstat -tulnp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 127.0.0.1:7115 0.0.0.0:* LISTEN 1520/node

tcp 0 0 127.0.0.1:7116 0.0.0.0:* LISTEN 1520/node

tcp 0 0 127.0.0.1:5037 0.0.0.0:* LISTEN 1616/adb

tcp 0 0 127.0.0.1:28015 0.0.0.0:* LISTEN 1430/rethinkdb

tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 1430/rethinkdb

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1070/sshd

tcp 0 0 127.0.0.1:29015 0.0.0.0:* LISTEN 1430/rethinkdb

tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1282/master

tcp 0 0 127.0.0.1:7111 0.0.0.0:* LISTEN 1519/node

tcp 0 0 127.0.0.1:7112 0.0.0.0:* LISTEN 1519/node

tcp 0 0 127.0.0.1:7113 0.0.0.0:* LISTEN 1519/node

tcp 0 0 127.0.0.1:7114 0.0.0.0:* LISTEN 1520/node

tcp6 0 0 ::1:28015 :::* LISTEN 1430/rethinkdb

tcp6 0 0 :::7120 :::* LISTEN 1550/node

tcp6 0 0 ::1:8080 :::* LISTEN 1430/rethinkdb

tcp6 0 0 :::22 :::* LISTEN 1070/sshd

tcp6 0 0 ::1:29015 :::* LISTEN 1430/rethinkdb

tcp6 0 0 ::1:25 :::* LISTEN 1282/master

tcp6 0 0 :::7100 :::* LISTEN 1591/node

tcp6 0 0 :::7102 :::* LISTEN 1574/node

tcp6 0 0 :::7103 :::* LISTEN 1580/node

tcp6 0 0 :::7104 :::* LISTEN 1586/node

tcp6 0 0 :::7105 :::* LISTEN 1556/node

tcp6 0 0 :::7106 :::* LISTEN 1562/node

tcp6 0 0 :::7110 :::* LISTEN 1568/node

udp 0 0 127.0.0.1:323 0.0.0.0:* 728/chronyd

udp6 0 0 ::1:323 :::* 728/chronyd

下面是解决方案,其实也就是一个方法而已

将上面脚本,做如下修改:

# stf_p.kill() # 不使用这个方法

os.killpg(os.getpgid(stf_p.pid), 9) 换成这个方法,这个方法作用是杀死一个进程组的所有子进程

运行后,等60秒,看下效果如何(我重启了linux服务器,执行脚本前所以stf是关闭状态):

脚本打印结果:

(venv) [root@localhost test_one]# python test_kill_popen.py

状态: None

开启进程的pid 1490

所属进程组的pid 1490

# -----------------

1490端口进程情况如下:

[root@localhost ~]# ps -aux | grep 1490

root 1651 0.0 0.0 112676 980 pts/2 S+ 13:35 0:00 grep --color=auto 1490

# -----------------

stf相关进程情况(端口我就不贴了,看端口就够明白了)

[root@localhost ~]# ps -aux | grep stf

root 1282 0.0 0.0 89544 2080 ? Ss 13:26 0:00 /usr/libexec/postfix/master -w

postfix 1283 0.0 0.0 89648 3996 ? S 13:26 0:00 pickup -l -t unix -u

postfix 1284 0.0 0.0 89716 4020 ? S 13:26 0:00 qmgr -l -t unix -u

root 1654 0.0 0.0 112676 980 pts/2 S+ 13:36 0:00 grep --color=auto stf

# -----------------

可以看出,进程结束的干干净净!!开心不?!

三、结论:

使用popen执行系统命令时,结果popen开启的进程,不要使用popen.kill()了,使用os.killpg()!

PS:os.kill(),这个也不好使,也只是杀死一个终端进程而已,并没有杀死由终端开启的子进程

结束语:

我还只是个python初学者,或者python里面有更好的方法,只是我还不知道,大家如果有好的方法,一定要告诉我哦,感激不尽!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值