subprocess.CREATE_NEW_PROCESS_GROUP
A Popen creationflags parameter to specify that a new process group will be created. This flag is necessary for using os.kill() on the subprocess.
This flag is ignored if CREATE_NEW_CONSOLE is specified.
比较高兴,以为能解决问题了,结果测试半天,才了解这玩意是only windows的,我去啊,不过想到了,win能做到的,linux肯定也可以,于是定位到
preexec_fn
又是一通google,不是对象吗,弄了个setpgid(0,0) 测试了,子进程还是和主调进程属于同一个进程组,后来灵机一动:
[liufeng@1.2.3.4 kill-subprocess]$ cat sub-process.py
import subprocess
import os
import time
def my_func():
#派生两个子进程,子进程里又派生几个sleep的孙子进程,主要是为了测试kill进程组。
run_str2 = ‘/bin/sh test.sh‘
run_str = ‘/bin/sh test_quick.sh‘
cmd2 = run_str.split()
cmd = run_str.split()
#测试了一些个preexec_fn的值,最终发现能用的,对python的对象的概念还是不理解啊,新手,新手。
#p = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = False, creationflags = subprocess.CREATE_NEW_PROCESS_GROUP)
#p = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = False, creationflags = 0)
p = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = False, preexec_fn = os.setpgrp )
p2 = subprocess.Popen(cmd2, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = False, preexec_fn = os.setpgrp )
#@p = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = False, preexec_fn = os.setpgid(0, 0) )
pid = p.pid
pgid = os.getpgid(pid)
print "pid: %d\n" %pid
print "pgid: %d\n" %pgid
return pid
pid = my_func()
#p.wait()
print "now , sleep 2s ,then , os.kill gpid %d" % pid
time.sleep(20)
a = os.kill(-pid, 9)
print "kill,return:"
print a
# kill的时候,我测试了kill 没有权限的root进程,会报错:权限不允许
# 测试了kill p p2 都可以kill
#a = os.kill(2445, 9)
#print "kill root process 2445 ,return:"
#print a
#p.wait()
#os.waitpid(pgid, 0)
# 2445 is a root process
#os.waitpid(2445, 0)
#os.waitpid(p2.pid, 0)
os.waitpid(pid, 0)
print "waitpid,return:"
print a
time.sleep(22)
print "done..."
#p.terminate()
#p.kill()
#p.wait()
#
#time.sleep(40)
#os.kill(pid, 9)