我如何在 bash 中创建一个 "stop" 脚本来关闭我之前用不同的 bash 脚本打开的 gnome 终端选项卡?

How do I make a "stop" script in bash that closes gnome-terminal tabs that I had previously opened with a different bash script?

我有一个名为 start.sh 的脚本:

#!/bin/bash

gnome-terminal --tab -- bash -c "./application1; bash"
gnome-terminal --tab -- bash -c "./application2; bash"
gnome-terminal --tab -- bash -c "./application3; bash"

此脚本在当前终端中打开三个新选项卡 window 并在每个选项卡中运行一个独特的应用程序。

现在我想编写 stop.sh,这将终止三个应用程序进程,然后关闭它们所在的三个选项卡 运行。因此脚本中的前三个命令可能是:

pkill -9 application1
pkill -9 application2
pkill -9 application3

但是我该如何关闭之前在其他脚本中打开的 gnome 终端选项卡?

每个应用程序都有一个bash作为父进程,所以你也应该杀死父进程。

您可以使用 ps 命令找到父级:

APP1PID=`pgrep application1`
APP1PPID=`ps j $APP1PID | awk 'NR>1 {print }'`
kill -9 $APP1PPID

杀死父进程就足够了,它也会杀死所有子进程。