Saturday, April 27, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 3190  / 2 Years ago, sun, april 10, 2022, 9:21:21

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.


More From » gnome-terminal

 Answers
1

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.



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

[#38260] Monday, April 11, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ndeecru

Total Points: 109
Total Questions: 128
Total Answers: 117

Location: Czech Republic
Member since Thu, Aug 11, 2022
2 Years ago
;