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.
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
get_parent()
{
ps --no-headers -p $1 -o ppid,cmd
}
process_list()
{
if [ "$1" -ne "1" ]; then
PARENT="$(get_parent $1 )"
printf "%s
" "$PARENT"
process_list $( printf "%s" "$PARENT" | awk '{print $1}' )
fi
}
print_header()
{
printf "PPID PROCESS
"
for i in $(seq 1 20 )
do
printf "-"
done
printf "
"
}
print_header
process_list $$
What I've found in the process is this:
$ bash get_process_list
PPID PROCESS
--------------------
31264 bash get_process_list
31251 mksh
16696 gnome-terminal
15565 /bin/mksh
2164 x-terminal-emulator
1677 init --user
1342 lightdm --session-child 12 19
1 lightdm
So we could use the two functions and grep
the gnome-terminal
, assuming that's what the user wants. If the user wants any terminal emulator, that may be problematic because aside from checking lsof
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:
$ bash get_process_list
PPID PROCESS
--------------------
32360 bash get_process_list
23728 -mksh
2164 tmux
1677 init --user
1342 lightdm --session-child 12 19
1 lightdm
tmux
apparently forks itself and the process gets picked up by init
, so again there's the obstacle.
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 PID
Sample run:
$ bash get_gt_pd.sh
XID:33554486 PID:20163 ACTIVE:true
XID:33554444 PID:20163 ACTIVE:false
And the code itself
get_gt_xid()
{ # Prints XID of each gnome-terminal window
qdbus --literal org.ayatana.bamf
/org/ayatana/bamf/matcher
org.ayatana.bamf.matcher.XidsForApplication
/usr/share/applications/gnome-terminal.desktop
}
for window in $(get_gt_xid | awk -F'{' '{ gsub(/,|}|]/," ");print $2 }' )
do
PID=$(qdbus org.ayatana.bamf /org/ayatana/bamf/window/"$window"
org.ayatana.bamf.window.GetPid)
ACTIVE=$( qdbus org.ayatana.bamf /org/ayatana/bamf/window/"$window"
org.ayatana.bamf.view.IsActive )
printf "XID:%s PID:%s ACTIVE:%s
" "$window" "$PID" "$ACTIVE"
done