Linux Mint Mate configure screensaver and display sleep via command line
Problem
A few days ago, while we were watching a film in VLC player in my laptop which runs Linux Mint 20.1 MATE, the screen went into screensaver mode. This happened multiple times and we had to press a button to make the screen come back. We have searched the web and we have seen that others had the same issue as seen here:
- Screensaver activates when watching movies - Linux Mint Forums
- Firefox activates screensaver when watching videos - Linux Mint Forums
After spending some time reading the experiences of other users in forums, we ended up disabling the screensaver and display sleep until our film was finished.
However we wanted to be able to do that by command line in bash, in order to be able to quickly toggle the setting. When watching films disable the screensaver and when using the computer for work to enable it.
Our preferred settings would be when working with the PC:
- Screensaver settings:
- Activate screensaver when computer is idle: true
- Regard the computer as idle after (minutes)
- Powermanagement settings:
- Put display to sleep when inactive for: 10 minutes
After some searching in the internet, we found out a related article for Ubuntu: 20.04 - How to configure ScreenSaver by Command Line? - Ask Ubuntu
gsettings command
While playing with the gsettings
command, we found the following:
To list all settings and write them to a file you can execute this command:
# List all settings and write them to allsettings.txt file
gsettings list-recursively > allsettings.txt
To read a single setting you can use the gsettings get
command:
chris@laptop:~$ gsettings get org.mate.power-manager sleep-display-ac
600
To set a single setting you can use the gsettings set
command:
chris@laptop:~$ gsettings get org.mate.power-manager sleep-display-ac 300
After playing with the values of Mate desktop “Screensaver” settings window we found the following settings:
- Screensaver settings:
- Activate screensaver when computer is idle (true/false):
org.mate.screensaver idle-activation-enabled <true/false>
- Regard the computer as idle after 5 minutes:
org.mate.session idle-delay 5
- Regard the computer as idle after Never (disabled):
org.mate.session idle-delay 0
- Activate screensaver when computer is idle (true/false):
- Power management settings:
- Put display to sleep when inactive for (seconds):
org.mate.power-manager sleep-display-ac 600
- Put display to sleep when inactive for Never (disabled):
org.mate.power-manager sleep-display-ac 0
- Put display to sleep when inactive for (seconds):
In configuration this is described as:
# Activate screensaver when computer is idle
org.mate.screensaver idle-activation-enabled true
# Regard the computer as idle after (minutes)
org.mate.session idle-delay 5
# Put display to sleep when inactive for: 10 minutes
org.mate.power-manager sleep-display-ac 600
# Put display to sleep when inactive for: Never
org.mate.power-manager sleep-display-ac 600
The script
We quickly drafted a bash script which toggles (enables or dsables) the screensaver and screen sleep you:
#!/bin/bash
currentSetting=`gsettings get org.mate.screensaver idle-activation-enabled`
if [ "$currentSetting" == "true" ]; then
# Activate screensaver when computer is idle
gsettings set org.mate.screensaver idle-activation-enabled false
# Regard the computer as idle after 0 minutes (Never)
gsettings set org.mate.session idle-delay 0
# Put display to sleep when inactive for: 0 seconds (Never)
gsettings set org.mate.power-manager sleep-display-ac 0
echo "Screensaver deactivated"
else
# Activate screensaver when computer is idle
gsettings set org.mate.screensaver idle-activation-enabled true
# Regard the computer as idle after 5 minutes
gsettings set org.mate.session idle-delay 5
# Put display to sleep when inactive for: 10 minutes
gsettings set org.mate.power-manager sleep-display-ac 600
echo "Screensaver activated"
fi
How does it work?
The script is pretty simple:
- It reads the
org.mate.screensaver idle-activation-enabled
configuration to check if the screensaver is currently enabled. - If the screensaver is enabled:
- it disables it.
- It also disables display sleep.
- If the screensaver is disabled:
- it enables it with idle time of 5 minutes.
- It also enables the display sleep after 10 minutes.
- It prints a message in
stdout
to inform us about the action it took.
Example usage
Put the script into a file, let us say myscreensaver.sh
, make it runnable with chmod 744 myscreensaver.sh
and test it:
chris@laptop:~$ # Calling it for the first time
chris@laptop:~$ ./myscreensaver.sh
Screensaver activated
chris@laptop:~$
chris@laptop:~$ # Calling it for the second time
chris@laptop:~$ ./myscreensaver.sh
Screensaver deactivated
I hope this helps. Have a wonderful day!
Comments