如何在 gnome 终端中终止子进程
How to kill subprocess in gnome terminal
我有三个 *.py 脚本命名为:
- terminal_starter,
- subprocess_in_terminal,
- ctrlc_sender
分别用以下代码:
terminal_starter.py
import subprocess
import os
p = subprocess.Popen(['gnome-terminal -e "python subprocess_in_terminal.py"'], shell=True)
gpid = os.getpgrp()
ppid = os.getpid()
p1 = subprocess.Popen(["python ctrlc_sender.py " + str(gpid) + " " + str(ppid)], shell=True)
while 1:
pass
subprocess_in_terminal.py
import time
while 1:
print "Subprocess in terminal."
time.sleep(1)
ctrlc_sender.py
import signal
import os
import sys
import time
gpid = sys.argv[1]
ppid = sys.argv[2]
for i in range(10):
print "Killer says: I will kill " + gpid + "and " + ppid
time.sleep(1)
os.killpg(int(gpid), signal.SIGTERM)
os.kill(int(ppid), signal.SIGTERM)
我想杀subprocess_in_terminal.py但是我杀不了
我 运行 这些脚本在 Ubuntu 16.04 LTS 和 Python 2.7.
如有任何帮助,我们将不胜感激。
gnome-terminal 似乎从当前 parent shell 和 "adopt" 中分离出来作为 parent 初始化进程。因此,进程之间的 parent-ancestor 连接被破坏,找到我们子进程的 parent 和 children 成为一项不平凡的任务。
但是,如果 gnome-terminal 可以替换为 "general" shell,那么我希望 terminal_starter.py 和 ctrlc_sender.py 中的一些小改动可能会有所帮助:
terminal_starter.py
1 import subprocess
2 import os
3
4 p = subprocess.Popen(['python subprocess_in_terminal.py'],preexec_fn=os.setsid, shell=True)
5
6 gpid = os.getpgrp()
7 ppid = os.getpid()
8
9 p1 = subprocess.Popen(["python ctrlc_sender.py " + str(gpid) + " " + str(ppid)], shell=True)
10
11 while 1:
12
13 pass
preexec_fn=os.setsid 点我们进程为进程组长
ctrlc_sender.py
1 import signal
2 import os
3 import sys
4 import time
5
6 gpid = sys.argv[1]
7 ppid = sys.argv[2]
8
9 for i in range(10):
10 print "Killer says: I will kill " + gpid + " and " + ppid
11 time.sleep(1)
12
13
14 import psutil
15
16 paren_proc = psutil.Process(int(ppid))
17
18 for child_proc in parent_proc.children(recursive=True):
19 child_proc.kill()
20 parent_proc.kill()
通过 psutil.Process 我们可以递归地杀死所有 children。之后我们终于可以杀死 parent 进程。
我有三个 *.py 脚本命名为:
- terminal_starter,
- subprocess_in_terminal,
- ctrlc_sender
分别用以下代码:
terminal_starter.py
import subprocess
import os
p = subprocess.Popen(['gnome-terminal -e "python subprocess_in_terminal.py"'], shell=True)
gpid = os.getpgrp()
ppid = os.getpid()
p1 = subprocess.Popen(["python ctrlc_sender.py " + str(gpid) + " " + str(ppid)], shell=True)
while 1:
pass
subprocess_in_terminal.py
import time
while 1:
print "Subprocess in terminal."
time.sleep(1)
ctrlc_sender.py
import signal
import os
import sys
import time
gpid = sys.argv[1]
ppid = sys.argv[2]
for i in range(10):
print "Killer says: I will kill " + gpid + "and " + ppid
time.sleep(1)
os.killpg(int(gpid), signal.SIGTERM)
os.kill(int(ppid), signal.SIGTERM)
我想杀subprocess_in_terminal.py但是我杀不了
我 运行 这些脚本在 Ubuntu 16.04 LTS 和 Python 2.7.
如有任何帮助,我们将不胜感激。
gnome-terminal 似乎从当前 parent shell 和 "adopt" 中分离出来作为 parent 初始化进程。因此,进程之间的 parent-ancestor 连接被破坏,找到我们子进程的 parent 和 children 成为一项不平凡的任务。
但是,如果 gnome-terminal 可以替换为 "general" shell,那么我希望 terminal_starter.py 和 ctrlc_sender.py 中的一些小改动可能会有所帮助:
terminal_starter.py
1 import subprocess
2 import os
3
4 p = subprocess.Popen(['python subprocess_in_terminal.py'],preexec_fn=os.setsid, shell=True)
5
6 gpid = os.getpgrp()
7 ppid = os.getpid()
8
9 p1 = subprocess.Popen(["python ctrlc_sender.py " + str(gpid) + " " + str(ppid)], shell=True)
10
11 while 1:
12
13 pass
preexec_fn=os.setsid 点我们进程为进程组长
ctrlc_sender.py
1 import signal
2 import os
3 import sys
4 import time
5
6 gpid = sys.argv[1]
7 ppid = sys.argv[2]
8
9 for i in range(10):
10 print "Killer says: I will kill " + gpid + " and " + ppid
11 time.sleep(1)
12
13
14 import psutil
15
16 paren_proc = psutil.Process(int(ppid))
17
18 for child_proc in parent_proc.children(recursive=True):
19 child_proc.kill()
20 parent_proc.kill()
通过 psutil.Process 我们可以递归地杀死所有 children。之后我们终于可以杀死 parent 进程。