【问题标题】:How to terminate a process running in parent terminal and in "n" child terminals in linux using python?如何使用python终止在父终端和linux的“n”子终端中运行的进程?
【发布时间】:2020-07-14 18:59:07
【问题描述】:

我在 Linux 终端中运行一个 python 脚本“./main.py”,它会自动启动另外两个终端并在其中运行两个不同的代码,我按照以下方式进行操作,

import os
import sys
import subprocess
import rospy
import psutil, sys
import signal

print(os.getpid())
arra = []
arra.append(os.getpid())
for i in range(2):
    if i == 0:     
        p = subprocess.Popen(['gnome-terminal', '--', 'roslaunch', 'ur5_notebook', 'main_r2_mt.launch', 'gui:=False'])
    if i > 0:
        p = subprocess.Popen(['gnome-terminal', '--', 'roslaunch', 'ur5_notebook', 'main_r2_mt.launch'])

    arra.append(p.pid)

rospy.sleep(20)

现在我还想杀死父终端以及我启动的所有子终端。我首先尝试了 sys.exit() 即

print(os.getpid())
arra = []
arra.append(os.getpid())
for i in range(2):
    if i == 0:     
        p = subprocess.Popen(['gnome-terminal', '--', 'roslaunch', 'ur5_notebook', 'main_r2_mt.launch', 'gui:=False'], preexec_fn=os.setpgrp)
    if i > 0:
        p = subprocess.Popen(['gnome-terminal', '--', 'roslaunch', 'ur5_notebook', 'main_r2_mt.launch'], preexec_fn=os.setpgrp)
# rospy.sleep(50)
    arra.append(p.pid)
# print(list(os.getgroups))
rospy.sleep(20)

sys.exit()

但这只会杀死当前/父终端,子终端仍然处于活动状态

然后我尝试杀死单个进程 ID (PID),我通过以下方式执行此操作

import os
import sys
import subprocess
import rospy
import psutil, sys
import signal

print(os.getpid())
arra = []
arra.append(os.getpid())
for i in range(2):
    if i == 0:     
        p = subprocess.Popen(['gnome-terminal', '--', 'roslaunch', 'ur5_notebook', 'main_r2_mt.launch', 'gui:=False'])
    if i > 0:
        p = subprocess.Popen(['gnome-terminal', '--', 'roslaunch', 'ur5_notebook', 'main_r2_mt.launch'])

    arra.append(p.pid)

rospy.sleep(20)

for i in reversed(arra):
     print(i)
     os.killpg(i, signal.SIGINT)

但是,只有当前的终端进程会被关闭,而子进程不会。请让我了解我可能在哪里犯错。我想编写一个脚本来关闭与父终端关联的所有终端。

【问题讨论】:

    标签: python python-3.x linux terminal


    【解决方案1】:

    您在使用您的代码时遇到问题,因为gnome-terminalgnome-terminal-server 发送了一个请求以启动一个新终端,然后它就退出了。您可以通过执行以下代码(您的代码的轻微变化)自行验证这一点,其中gnome-terminal 在一个进程中调用,xterm 在另一个进程中调用:

    import os
    import sys
    import subprocess
    import psutil, sys
    import signal
    import time
    
    print(os.getpid())
    
    arra = []
    #arra.append(os.getpid())
    
    for i in range(2):
        if i == 0:     
            p = subprocess.Popen(['gnome-terminal', '--', 'yes'])
            print ("gt: " + str(p.pid))
        if i > 0:
            p = subprocess.Popen(['xterm', '-hold', 'yes'])
            print("xt: " + str(p.pid))
        arra.append(p.pid)
    
    for i in reversed(arra):
        print("next kill:" + str(i))
        time.sleep(60)
        os.kill(i, signal.SIGTERM)
    

    如果您在执行上述代码时在另一个 shell 中调用 ps -e | grep gnome-terminal,您将看到 gnome-terminal 已失效,因此您无法杀死它。另一方面,如果您使用 xterm,例如,一切都会按您的预期工作。

    样本输出:

    gt: 26054
    xt: 26055
    next kill:26055
    
    -----------------------------
    
    $ ps -e | grep gnome-terminal
    26054 pts/2    00:00:00 gnome-terminal <defunct>
    

    因此,一种选择是使用xterm 来执行您的代码或具有类似行为的其他类型的终端。

    【讨论】:

      猜你喜欢
      • 2020-10-13
      • 2018-09-23
      • 2016-04-12
      • 1970-01-01
      • 2011-12-30
      • 2014-08-06
      • 2017-08-18
      • 2021-11-26
      • 2014-02-09
      相关资源
      最近更新 更多