Score:2

Finding out all running GUI applications

cn flag

There are some old questions similar to this; however, I haven't found a way to list and optionally close applications that are also minimized to system tray.

During a session log out in Windows, Windows gracefully asks all running GUI applications to gracefully terminate and only when all applications terminate normally and acknowledge this request Windows continues the log out process.

On the contrary, in Ubuntu, GNOME does not ask all applications to terminate gracefully, but just kills them during log out. There may be cases when you just don't notice that you have an editor open with unsaved data and in such a case you may lose all your updates. Or there may be some other GUI applications that need to be closed gracefully or have to continue running until some operation is finished.

For this reason, I created a script to find out all open GUI windows and then "gracefully" close them one by one. The key to this script is the command wmctrl -lp for listing the applications and wmctrl -ic <win_id> for sending a close message to an application whose window id is known. However, wmctrl -lp fails to list any running applications that are minimized to system tray and thus have currently no window (and window id). Some sample applications that I use and can be minimized to system tray are: Viber (.deb package), Telegram (also .deb package) or some Wine (Windows) applications. Note that I am using the snap version of Firefox and wmctrl successfully detects its window id, even though Firefox' parent seems to be systemd.

I am currently using Xorg and prefer to continue using Xorg. But, I have tested with Wayland too and have seen worse results: in Wayland wmctrl -lp fails to list most "normal" applications that are easily detected in Xorg.

Note: I have tested and I have seen that a minimized to tray application loses its window id. When restored (by interacting with the mouse on its icon on the system tray) it will get a new window id.

How can I get a list of all the minimized to tray applications and restore their windows so that I can manipulate them with wmctrl?

sudodus avatar
jp flag
In Ubuntu 22.04.2 LTS I use `wmctrl`, so it was easy for me to run `wmctrl -lp`. It will list Firefox as well as a minimized gnome-terminal window (when three are running). Please give an example where an application is not listed when minimized. For example, which editor are you using? - Does it make a difference if you switch between Wayland and X?
sudodus avatar
jp flag
I use the editor `geany` and it is also listed by `wmctrl -lp` when minimized. And I use X (not Wayland).
FedKad avatar
cn flag
There is no **minimize to system tray** option in `geany`. I am not talking only about editors; I need to find *all* GUI type applications.
sudodus avatar
jp flag
I see, I tested with `onboard` that I have, and which can be minimized to system tray, and yes, it will disappear from the list by `wmctrl -lp` for me. I see the problem but have no solution, at least not right now :-(
sudodus avatar
jp flag
A crude workaround way would be to list the GUI applications that you use and test with `ps` for those (in a shellscript). Or at least list and test for the applications that can be minimized to system tray.
karel avatar
sa flag
viber and telegram have snap packages (for example viber-unofficial and telegram-desktop). Did you install viber and telegram as snap packages?
Score:1
jp flag

The following script guir works with the help of a manual list manlist with application programs, that are not shown by wmctrl -lp.

#!/bin/bash

manlist=\
'Viber
Telegram
onboard
skype
xeyes'

mizer=$(wmctrl -lp|grep -v '@!0,0;BDHF'|tr -s ' ' '\t'|cut -f3|grep -v '^0$')

guip="$mizer
$manlist"

runners=$(ps -e |grep "^$guip$"|sed -e 's/^ *//' -e 's/ .*$//')
guir="$(ps $runners)"
echo "$guir"
if [  $(echo "$runners"|wc -l) -gt 1 ]
then
 echo "---------------------------------------------"
 echo "Please close some programs gracefully before logout/reboot/shutdown"
 exit 1
fi

Test run:

$ ./guir 
    PID TTY      STAT   TIME COMMAND
   3006 ?        Ssl    0:59 /usr/libexec/gnome-terminal-server
   3042 pts/0    S+     0:58 xeyes -geometry +1775-0
   3043 pts/0    S+     0:58 xeyes -geometry +800+0
   3954 pts/0    Sl    10:02 /snap/firefox/2605/usr/lib/firefox/firefox
   5790 ?        Sl     3:39 /usr/lib/thunderbird/thunderbird
   6707 pts/0    Sl     1:18 geany
   9929 ?        Sl     0:07 /usr/bin/python3 /usr/bin/onboard
---------------------------------------------
Please close some programs gracefully before logout/reboot/shutdown
  • If you want to use this method, you will probably modify this script, for example by adding some programs to manlist.

  • Assuming the script is run in a terminal window, one program is allowed without asking to close some programs. You may want to change that too.

  • You can close xeyes without saving anything, it is in manlist only as a demo because wmctrl does not find its process number (lists 0), and there are probably other programs that are affected by something similar.

  • Finally, I think that you intend to use your script/program automatically in your logout/reboot/shutdown processes, and for that reason I modify the exit status when programs should be closed gracefully.

FedKad avatar
cn flag
Thanks for the script. I am looking for a more neat and automatic solution though. Also I want to send a close message to each of these windows using `wmctrl -i -c <WIN_ID>` and I have already written a similar script (it misses these minimized to tray windows though).
sudodus avatar
jp flag
@FedKad, Is that closing method graceful enough to save the content for example in open editors? Well. I guess you have tested that. But it is still only working where the process is seen by `wmctrl`. I guess some processes can be killed more or less gracefully (with different `kill' options, but some processes might need manual action in order to avoid losing 'work in progress'. - Anyway that is why I thought that you ask this question.
sudodus avatar
jp flag
And thanks for the edits :-) I hope that the answer can help you creating the tool you want (I know that it is not a whole solution).
FedKad avatar
cn flag
Yes, closing an editor with `wmctrl -i -c <WIN_ID>` is graceful enough to display the editor's **Save, Discard, Cancel** prompt.
sudodus avatar
jp flag
@FedKad, I notice that if I try to restart the 'system-trayed' Onboard, there will not be a second instance, but it will get active with a window again, and then `wmctrl -i -c <WIN_ID>` will work on it. - So when a 'system-trayed' program that behaves like that is found, 'restart it' (in the script) and get a window ... - Telegram (via snap in 23.04) behaves like Onboard but Skype (via snap in 23.04) must be killed according to my tests.
FedKad avatar
cn flag
Yes; that is a good observation. But, I wish I had a flexible method of getting a complete list of all currently "system-trayed" applications. I am looking at **Ubuntu AppIndicators** extension right now...
Score:0
it flag

Since all GUI processes are children (grandchildren, ...) of the window manager, you can use pgrep to find the PID of your window manager, and ps or pstree to list children of that PID. Read man pgrep ps pstree.

FedKad avatar
cn flag
This is not so easy: There are snaps (like Firefox) that don't seem to be children of `gnome-shell`. Also, I corrected my Question by clarifying that the missing applications are the ones *minimized **to system tray***.
pierrely avatar
cn flag
echo $(pidof firefox) and you have to parse the list of all returned when more than one. do for i in $(echo $(pidof firefox)); do
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.