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.
automationgnome-terminalscripts
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.
Best Answer
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
grep
thegnome-terminal
, assuming that's what the user wants. If the user wants any terminal emulator, that may be problematic because aside from checkinglsof
for a pts device open, there's no way to tell whether or not the process is a terminal emulator.Aside from that , there is something very interesting as well:
tmux
apparently forks itself and the process gets picked up byinit
, so again there's the obstacle.Using Unity's Ayatana
The code bellow uses
qdbus
and Ayatana's dbus interface to list all gnome-terminal windows and whether they are focused at the moment or not. This can be later parsed or edited to output only active/focused window PIDSample run:
And the code itself