Ubuntu – Track and close children terminals. Spare the parent

bashcommand linegnome-terminalprocessscripts

I would like to run a data collection script which, for each loop:

  1. open new terminal(s) to run robot simulation processes in them

  2. run another process2 in the parent terminal

  3. when the process2 finishes, close all the simulator terminals opened in step1

I have read through many other Q&A's here about closing terminals. What I struggle with is that I want to keep my parent terminal open with the parent process running at all times. Ideally, I would like to be able to track each child terminal that my script opens, such that I can choose to close them later. However, I can also live with just closing all the other terminals without checking if they are the children of the current script, if that will make my life easier.

I have very limited knowledge about how each terminal is identified. I know that each process has one process ID, but what about a terminal? When and how is that different from a process (please see the example below that gives me confusion about terminal/process ID-ing)?

I tried writing the script below using the answers from other related Q&A's.

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

I tried running the above script by initially having another blank terminal open beside the current one (so, 2 total). Here is the result:

  1. The output (below) lists 3 terminals, with the script trying to close the parent terminal (giving me a popup window to confirm killing). Why does it list 3 and not 2 terminals? It seems like the parent terminal gives 2 different IDs. What are these IDs?
  2. It half of the time successfully closes the other blank terminal but sometimes it does not even kill the other terminal (even though, based on the output, it should have attempted to kill 2 out of the 3 listed terminals).
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

Similarly, if I test the script with only one (the parent) terminal open, it lists 2 terminals and tries to close the parent:

parent_terminal: 62914571 
This is NOT parent_terminal 62914571 
Killing 62914561 
This is parent_terminal 62914571 
Do not kill 62914571

Added: I have also looked at this Q&A: Closing a Specific Terminal, and tried running the below command in a few terminals that I open manually.

$ cat /proc/$$/status | grep PPid

However, all terminals give me the same ID (PPid: 2298).

Best Answer

Instead of closing the terminals, I recommend that you somehow terminate (e.g. kill) the processes running inside them. These are your robot simulation processes, started up by you, so you should know the PID belonging to them.

Given the default of every terminal emulator, which is to close itself when the requested process (i.e. ./robot_sim) terminates, this will result in those terminal windows or tabs closing.

Related Question