I have my work computer in an open space. Is it possible to configure ubuntu so that when I login after inactivity all windows be in minimized state and the PC shows the desktop? Ubuntu 20.04 / 22.04
-
To login, you have to log out first. If you log out, all Windows will be closed. Then there will be nothing to embarrass you in front of your colleagues. :) – user68186 Aug 01 '22 at 12:51
-
1do you mean if the computer locks itself from inactivity, and you have to unlock it? – Esther Aug 01 '22 at 13:19
-
@Esther yes this is exactly what I mean. I thought it was clear from the title – ECII Aug 01 '22 at 13:46
-
https://unix.stackexchange.com/a/449013/517866 gives a way of writing as script when the device locks/unlocks. Perhaps you can write a script that minimizes all windows and run it on lock or unlock (which is not the same thing as logging in/out, so you might want to edit your question to clarify that) – Esther Aug 01 '22 at 13:53
1 Answers
I have tried this in Ubuntu 20.04. It seems to work.
Required software
You will need the utility wmctrl
and gnome-screensaver
. If they are not installed, open a terminal using Ctrl+Alt+T use the following command to install them.
sudo apt install wmctrl gnome-screensaver
Edit .profile
Find the file .profile
in your Home folder. This is a hidden file. Open the Files app (AKA Nautilus) and press Ctrl+H to see all the hidden files.
Add the following line at the end of the file /home/$USER/.profile
:
set | grep DBUS_SESSION_BUS_ADDRESS > ~/.DBUS_SESSION_BUS_ADDRESS
Save the file.
Log out and log back in
The .profile
is executed when you login to your computer. Logging out (not locking the computer due to inactivity) and logging in will ensure the changes you made in the .profile
is used.
Create the script /home/$USER/bin/unlock_min
You may call the script something else or place it in some other folder, but you will need to adjust the next step accordingly. Here is the script:
#!/bin/bash
# Purpose: Minimize all open windows if the screen is locked
source /home/$USER/.DBUS_SESSION_BUS_ADDRESS
export DBUS_SESSION_BUS_ADDRESS
echo $DBUS_SESSION_BUS_ADDRESS
if (/usr/bin/gnome-screensaver-command -q | /bin/grep "is active");
then
/usr/bin/wmctrl -k on
fi
Save this script as /home/$USER/bin/unlock_min
, where you can replace $USER
with your username.
Create a new cron job
You will have this script run every minute to check if the screen is locked. To do this edit your cron jobs using the crontab
utility. Use the command:
crontab -e
When you use this command for the first time it may ask you to select a text editing utility. We will use the default nano
. You may want to familiarize yourself with it.
Add the following line at the end of crontab file opened by the command above:
* * * * * env DISPLAY=:0 /bin/bash /home/$USER/bin/unlock_min > /home/$USER/log.txt 2>&1
Note, you may have to change $USER
to your username. This line will run the command every minute and write the output in the file /home/$USER/log.txt
. If all goes well the log.txt
will have a line like:
unix:path=/run/user/1000/bus
If there is no log.txt
or if it contains something else, then something has gone wrong.
Try it
Open some sensitive windows on your computer. Select the Lock from the System Menu (top right corner of your desktop).
Go get a cup of tea/coffee.
Unlock your computer using your password. All the open windows should be minimized.
References:
Hope this helps

- 33,360
-
1
-
@ECII Thanks for the edits. I didn't realize
gnome-screensaver
may not be installed by default. – user68186 Aug 08 '22 at 13:59 -