2015-09-14 42 views
2

我从Python运行一个bash脚本,产生一些长时间运行(在前台)的进程。但是,如果我用python杀死了子进程对象,那么脚本产生的(子)进程就会变成孤儿而不是被杀死。Python子流程kill()让孩子孤儿

process=subprocess.Popen(['/bin/bash','script']) #this runs in background 
[....] 
process.terminate() 

'脚本' 派生 “过程1 |过程2 | ...”

terminate()运行过程链

(过程1 |过程2 ...)被孤立和PID1下运行。我想杀死他们

回答

0
process=subprocess.Popen(['/bin/bash','script']) 
pid = process.pid # get pid of process 

杀死他们

os.system("kill -9 " + str(pid))