跟踪并关闭儿童终端。饶了父母

Vic*_*con 5 command-line bash scripts gnome-terminal process

我想运行一个数据收集脚本,对于每个循环:

  1. 打开新终端以在其中运行机器人模拟过程

  2. 在父终端中运行另一个 process2

  3. 当 process2 完成后,关闭所有在 step1 中打开的模拟器终端

我已经阅读了许多其他关于关闭终端的问答。我挣扎的是,我希望在父进程始终运行的情况下保持父终端打开。理想情况下,我希望能够跟踪我的脚本打开的每个子终端,以便我可以选择稍后关闭它们。但是,我也可以只关闭所有其他终端而不检查它们是否是当前脚本的子项,如果这会让我的生活更轻松。

我对如何识别每个终端的了解非常有限。我知道每个进程都有一个进程 ID,但是终端呢?它何时以及如何与进程不同(请参阅下面的示例,该示例让我对终端/进程 ID 感到困惑)?

我尝试使用其他相关问答的答案编写下面的脚本。

parent_terminal=$(xdotool getactivewindow)
echo "parent_terminal: $parent_terminal"

# TODO: start external loop

    # open a child terminal(s) and run robot_sim there
    #(gnome-terminal -e './robot_sim') 
    #echo $! --> gives blank.. 

    #./process2
    #echo "process2 done. Killing robot_sim.."

    # Kill all other terminals except this one (the parent)
    xdotool search --class "terminal" | while read id
    do
          if [ "$id" -eq "$parent_terminal" ]; then
            echo "This is parent_terminal $parent_terminal"
            echo "Do not kill $id"
            continue
          else
            echo "This is NOT parent_terminal $parent_terminal"
            echo "Killing $id"
            xdotool windowactivate "$id" &>/dev/null # Make the terminal with $id active
            xdotool key ctrl+shift+q # Kill terminal by simulating a key press
            #sleep 0.2
          fi
    done 

# done external loop
Run Code Online (Sandbox Code Playgroud)

我尝试通过最初在当前终端旁边打开另一个空白终端来运行上述脚本(因此,总共 2 个)。结果如下:

  1. 输出(如下)列出了 3 个终端,脚本试图关闭父终端(给我一个弹出窗口以确认终止)。为什么它列出了 3 个而不是 2 个终端?似乎父终端给出了 2 个不同的 ID。这些 ID 是什么?
  2. 它有一半的时间成功关闭了另一个空白终端,但有时它甚至没有杀死另一个终端(即使根据输出,它应该试图杀死 3 个列出的终端中的 2 个)。
parent_terminal=$(xdotool getactivewindow)
echo "parent_terminal: $parent_terminal"

# TODO: start external loop

    # open a child terminal(s) and run robot_sim there
    #(gnome-terminal -e './robot_sim') 
    #echo $! --> gives blank.. 

    #./process2
    #echo "process2 done. Killing robot_sim.."

    # Kill all other terminals except this one (the parent)
    xdotool search --class "terminal" | while read id
    do
          if [ "$id" -eq "$parent_terminal" ]; then
            echo "This is parent_terminal $parent_terminal"
            echo "Do not kill $id"
            continue
          else
            echo "This is NOT parent_terminal $parent_terminal"
            echo "Killing $id"
            xdotool windowactivate "$id" &>/dev/null # Make the terminal with $id active
            xdotool key ctrl+shift+q # Kill terminal by simulating a key press
            #sleep 0.2
          fi
    done 

# done external loop
Run Code Online (Sandbox Code Playgroud)

同样,如果我只在一个(父)终端打开的情况下测试脚本,它会列出 2 个终端并尝试关闭父:

parent_terminal: 62914571 
This is NOT parent_terminal 62914571 
Killing 62914561 
This is NOT parent_terminal 62914571 
Killing 62920887 
This is parent_terminal 62914571 Do not kill 62914571
Run Code Online (Sandbox Code Playgroud)

补充:我也看过这个问答:关闭特定终端,并尝试在我手动打开的几个终端中运行以下命令。

$ cat /proc/$$/status | grep PPid
Run Code Online (Sandbox Code Playgroud)

但是,所有终端都给我相同的 ID(PPid:2298)。

egm*_*ont 2

我建议您以某种方式终止(例如终止)终端内运行的进程,而不是关闭终端。这些是你的机器人模拟进程,由你启动,所以你应该知道属于它们的PID。

考虑到每个终端仿真器的默认设置是在请求的进程(即)终止时自行关闭./robot_sim,这将导致这些终端窗口或选项卡关闭。