How can I get the PID of the GNOME Terminal instance I'm using from within a Bash script?
I can run
xprop _NET_WM_PID and then manually click the terminal window, but I'd like to completely automate this.
I've written two recursive functions that trace parents of a process
What I've found in the process is this:
So we could use the two functions and
Aside from that , there is something very interesting as well:
Using Unity's Ayatana
The code bellow uses
And the code itself
To be safe though, the following will find the parent gnome-terminal process even if multiple bash shells are nested:
The following universal version will work for any shell, even if other grep instances are running. Deciphering it is left as an exercise for the reader ;)
pstree -p -a -s \ $(pstree -p -a | grep -B3 $RANDOM$RANDOM \ | grep -m1 `echo $SHELL |cut -d/ -f3` | cut -d, -f2)\ | grep gnome-terminal | cut -d, -f2
This solution feels the most robust to me. It recursively looks up the parent PID until finding one that belongs to GNOME Terminal.