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
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
- 37,461