I am running voor some time my own ubuntu 24.04 LTS server on an old laptop. What i setup al long time ago is disable the system shutdown when closing the lid, but lately i found out that the screen stay’s on when the lid was closed! That was not what i was aiming for when i set that up. 🤔
So i fixed that too. But that was a bit harder. So here for my own documentation, and maybe for some others who bumps into this. My how to:
Setup laptop to stay on when the lid is closed.
To set the laptop up so that it keeps working when de lid is closed is easy: do the following:
- sudo nano /etc/systemd/logind.conf
- Change the line with the text HandleLidSwitch so that it becomes:
HandleLidSwitch=ignore
- If that line does not exists, add it.
- Save that file, and give the command:
- sudo service systemd-logind restart
That should do the first step.
Setup to turn off/on screen when lid is closed/opened
OK, next step takes a little bit more work:
- Install and configure acpid (this is the predecessor of acpi-support)
sudo apt-get install acpid
sudo systemctl enable acpid
sudo systemctl start acpid
sudo systemctl status acpid
- After this, we need to find out 2 things: what ACPI action belongs to closing/opening the lid: Start the command below, and close/open the lid. The command gives respective output:
$ acpi_listen
button/lid LID close
button/lid LID open
- The other thing is the type of backlight your system is using (most likely intel_backlight, but to be sure):
$ ls /sys/class/backlight/
intel_backlight
Ok, with that defined, we need to create two files, 1: the trigger file, 2: the script file. We start with the first one:
sudo nano /etc/acpi/events/lid -> Add following text:
event=button/lid.*
action=/etc/acpi/lid.sh
After creating this file, do the command: “sudo systemctl restart acpid” to activate the event file. You do not need to do this again, as change’s to the file lid.sh do not affect the acpid.
The 2nd file does all the work. Since i love some logging, i added that also:
sudo nano /etc/acpi/lid.sh -> Add following tekst:
#!/bin/bash
# Fill in your username
USER="<username>"
LOGFILE="/home/$USER/bin/log/acpid.log"
export DISPLAY=:0
export XAUTHORITY=/home/$USER/.Xauthority
lid_state=$(awk '{print $2}' /proc/acpi/button/lid/LID0/state)
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
if [ "$lid_state" = "closed" ]; then
sudo -u "$USER" bash -c "echo '[$timestamp] Lid closed - turning screen off' >> '$LOGFILE'"
sudo -u "$USER" DISPLAY=:0 XAUTHORITY="/home/$USER/.Xauthority" xset dpms force off
elif [ "$lid_state" = "open" ]; then
sudo -u "$USER" bash -c "echo '[$timestamp] Lid opened - turning screen on' >> '$LOGFILE'"
sudo -u "$USER" DISPLAY=:0 XAUTHORITY="/home/$USER/.Xauthority" xset dpms force on
else
sudo -u "$USER" bash -c "echo '[$timestamp] Unknown lid state: $lid_state' >> '$LOGFILE'"
fi
Works like a charm. Credits for the script (partly) and the image go to chatgpt. 😊