Ubuntu – Suspend on lid close after timeout

gnomelidpower-managementsuspend

I am using a laptop with Ubuntu 16.04, Gnome as desktop environment and LightDM as login manager.

What I want to do is to suspend the computer after the lid has been closed for 30 seconds. Is this possible?

Best Answer

Switch to suspend 30 seconds after the lid is closed


Disable default close-lid actions

To set a specific time, before the computer should switch to suspend, we need to disable the default actions for closing the lid. This can be done by the following commands:

gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action "nothing"

and

gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action "nothing"

Custom actions on closing lid

Subsequently, we need to set alternative actions on closing the lid, to include the break of 30 seconds before suspend. We can hook those to existing events.

The question is if the advantages of that weigh up to the more complicated setup that would require. A scripted version like below consumes practically zero from both your processor and your memory, even if you ran ten of them.

The advantage of the scripted option is that it is easily revertible; simply don't run it anymore and restore your original (or different) settings.

The script

#!/usr/bin/env python3
import time
import subprocess

# set delay time below (seconds)
delay = 30
# set path to lid status file
f = "/proc/acpi/button/lid/LID/state"
# --- set close command below 
close_command = ["systemctl", "suspend"]

def get_state():
    return "open" in open(f).read()

state1 = get_state()

while True:
    time.sleep(3)
    state2 = get_state()
    if state2 != state1:
        t = 0
        while not get_state():
            time.sleep(1); t = t+1
            if t > delay:
                subprocess.Popen(close_command)
                break
    state1 = state2

How to use

  1. As mentioned, first disable the default lid-close-actions:

    gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action "nothing"
    

    and

    gsettings set org.gnome.settings-daemon.plugins.power lid-close-battery-action "nothing"
    
  2. Copy the script into an empty file, save it as delay_lidsuspend.py

  3. In the head section of the script, some default values and paths are set. Most likely, you don't need to change anything:

    # set delay time below (seconds)
    delay = 30
    # set path to lid status file
    f = "/proc/acpi/button/lid/LID/state"
    # --- set close command below 
    close_command = ["systemctl", "suspend"]
    

    I am not sure the path to your lid state file ("/proc/acpi/button/lid/LID/state") is the same on every laptop. Test with the command:

    cat /proc/acpi/button/lid/LID/state
    
  4. Test- run the script by running from a terminal:

    python3 /path/to/delay_lidsuspend.py
    

    and close the lid to see if it works as intended (here it did the job perfectly).

  5. If all works fine, add it to Startup Applications.