Linux Mint Mate configure screensaver and display sleep via command line

4 minute read

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:

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:

  1. Screensaver settings:
    • Activate screensaver when computer is idle: true
    • Regard the computer as idle after (minutes)
  2. 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:

  1. Screensaver settings:
    1. Activate screensaver when computer is idle (true/false): org.mate.screensaver idle-activation-enabled <true/false>
    2. Regard the computer as idle after 5 minutes: org.mate.session idle-delay 5
    3. Regard the computer as idle after Never (disabled): org.mate.session idle-delay 0
  2. Power management settings:
    1. Put display to sleep when inactive for (seconds): org.mate.power-manager sleep-display-ac 600
    2. Put display to sleep when inactive for Never (disabled): org.mate.power-manager sleep-display-ac 0

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:

  1. It reads the org.mate.screensaver idle-activation-enabled configuration to check if the screensaver is currently enabled.
  2. If the screensaver is enabled:
    1. it disables it.
    2. It also disables display sleep.
  3. If the screensaver is disabled:
    1. it enables it with idle time of 5 minutes.
    2. It also enables the display sleep after 10 minutes.
  4. 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