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.. 

    #echo "process2 done. Killing robot_sim.."

    # Kill all other terminals except this one (the parent)
    xdotool search --class "terminal" | while read id
          if [ "$id" -eq "$parent_terminal" ]; then
            echo "This is parent_terminal $parent_terminal"
            echo "Do not kill $id"
            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

# 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).

  • Awesome first question. Welcome to AU!
    – grooveplex
    Jan 25, 2017 at 22:31
  • 1
    "all terminals give me the same ID" ... that is correct, GNOME Terminal starts a daemon which opens windows as needed, so all windows belong to the same process. There used to be an option to disable that, but it seems to be gone now.
    – muru
    Jan 26, 2017 at 1:57
  • 1
    @muru You can start up multiple gnome-terminal servers (daemons) with different App IDs, and get each of them open a window, as shown at wiki.gnome.org/Apps/Terminal/Debugging (without the gdb bits, obviously).
    – egmont
    Jan 26, 2017 at 14:07
  • @egmont that's the odd part. 16.04's manpage doesn't list that option, where's 14.04's did. I'll test it out, though.
    – muru
    Jan 26, 2017 at 14:10
  • 1
    While the parent terminal has the same pid, you can manage its children t by their window id. Will take a proper read later, am on mobile :\ Jan 26, 2017 at 16:41

1 Answer 1


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .