Search code examples
linuxbashunixtabsgnome-terminal

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


I have a script called 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"

This script opens three new tabs in the current terminal window and runs a unique application in each one.

Now I want to write stop.sh, which will kill the three application processes and then close the three tabs they were running in. So the first three commands in the script will probably be:

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

But how do I then close the gnome-terminal tabs I had previously opened in the other script?


Solution

  • Each application has a bash as the parent process, so you should kill the parent as well.

    You may find the parent with ps command:

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

    It is sufficient to kill the parent process, it will kill all children as well.