python subprocess.Popen 杀死带有子进程的进程
python subprocess.Popen kill process with child processes
如何发送 Ctrl-C 来处理或终止带有子进程的进程?
我的代码示例 (python 2.7):
# --*-- coding: utf-8 --*--
import subprocess
import os
import signal
proc = subprocess.Popen(['ping localhost'],shell=True,stdout=subprocess.PIPE)
print proc.pid
a = raw_input()
os.killpg(proc.pid, signal.SIGTERM)
我在 运行 编程时看到下一个进程:
user 16078 0.0 0.0 4476 916 pts/6 S+ 14:41 0:00 /bin/sh -c ping localhost
user 16079 0.0 0.0 8628 1908 pts/6 S+ 14:41 0:00 ping localhost
程序输出:
16078
raw_input之后:
Traceback (most recent call last):
File "subproc2.py", line 10, in <module>
os.killpg(proc.pid, signal.SIGTERM)
OSError: [Errno 3] No such process
我想杀死进程 pid 16078 和 pid 16079。
我该怎么做,程序中有什么错误?感谢帮助。
How would I do this and what the bug in program?
如果你想杀死进程组中的所有进程,那么你应该使用父进程id。像那样:
os.killpg(os.getpid(), signal.SIGTERM)
如果你只想杀死一个子进程,那么使用这个:
os.kill(proc.pid, signal.SIGTERM)
如何发送 Ctrl-C 来处理或终止带有子进程的进程?
我的代码示例 (python 2.7):
# --*-- coding: utf-8 --*--
import subprocess
import os
import signal
proc = subprocess.Popen(['ping localhost'],shell=True,stdout=subprocess.PIPE)
print proc.pid
a = raw_input()
os.killpg(proc.pid, signal.SIGTERM)
我在 运行 编程时看到下一个进程:
user 16078 0.0 0.0 4476 916 pts/6 S+ 14:41 0:00 /bin/sh -c ping localhost
user 16079 0.0 0.0 8628 1908 pts/6 S+ 14:41 0:00 ping localhost
程序输出:
16078
raw_input之后:
Traceback (most recent call last):
File "subproc2.py", line 10, in <module>
os.killpg(proc.pid, signal.SIGTERM)
OSError: [Errno 3] No such process
我想杀死进程 pid 16078 和 pid 16079。
我该怎么做,程序中有什么错误?感谢帮助。
How would I do this and what the bug in program?
如果你想杀死进程组中的所有进程,那么你应该使用父进程id。像那样:
os.killpg(os.getpid(), signal.SIGTERM)
如果你只想杀死一个子进程,那么使用这个:
os.kill(proc.pid, signal.SIGTERM)