2

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?

6
  • 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
    Commented May 7, 2023 at 9:36
  • I use the editor geany and it is also listed by wmctrl -lp when minimized. And I use X (not Wayland).
    – sudodus
    Commented May 7, 2023 at 9:45
  • 1
    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.
    – FedKad
    Commented May 7, 2023 at 10:05
  • 1
    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
    Commented May 7, 2023 at 10:25
  • 1
    viber and telegram have snap packages (for example viber-unofficial and telegram-desktop). Did you install viber and telegram as snap packages?
    – karel
    Commented May 7, 2023 at 10:33

2 Answers 2

1

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.

6
  • 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).
    – FedKad
    Commented May 8, 2023 at 16:28
  • @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
    Commented May 8, 2023 at 16:41
  • 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).
    – sudodus
    Commented May 8, 2023 at 16:44
  • 1
    Yes, closing an editor with wmctrl -i -c <WIN_ID> is graceful enough to display the editor's Save, Discard, Cancel prompt.
    – FedKad
    Commented May 8, 2023 at 17:17
  • 1
    @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.
    – sudodus
    Commented May 8, 2023 at 22:02
0

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.

2
  • 1
    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.
    – FedKad
    Commented May 7, 2023 at 9:13
  • 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
    – pierrely
    Commented May 8, 2023 at 22:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .