1

It is possible to control the behaviour of the power button using the 'HandlePowerKey' option in logind.conf. This is set to 'suspend'. A simple tap of the power button works as expected, suspending the OS. However, a long hold of the button seemingly overrides it and shuts down the computer. How does this occur?

Reason I am asking, is it appears that a script in /lib/systemd/system-sleep does not execute correctly on long-hold power.

2
  • 1
    Does this Super User QA answer your question? Holding down the power button to shut off is part of the motherboard's firmware.
    – Thegs
    Aug 16, 2021 at 16:57
  • Yes, thanks. Not sure why the force-shutdown interferes with my script though - there should be enough time to execute the suspend commands before the force-shutdown moves in.
    – Cheetaiean
    Aug 16, 2021 at 17:06

2 Answers 2

3

The power button is handled by the system firmware; typical configurations are as follows:

  • when the button is pressed and then released, the firmware sends a power even to the operating system, triggering either an orderly shutdown or a suspend;

  • when the button is pressed and not released for a number of seconds, the firmware powers the system down without informing the operating system.

The two sequences are either/or; the system isn’t asked to suspend and then shut down. That’s why your scripts don’t run when the power button is held for a long time: the operating system never finds out about it.

2

There are two things at play here.

First, as Stephen suggested, the firmware of your motherboard handles the signal from the button. Based on how long your press the button, the firmware will decide what to do.

The firmware will then send an ACPI signal to the operating system. A single quick press of the power button will send a power button signal. Pressing the reset button will send the reset signal. And holding the button will cause the firmware to cut power to the system without sending any signal to the operating system, which doesn't give the OS time to shut down in an orderly manner. In other words, all the scripts and triggers that normally fire on shutdown will not fire, because the firmware never sent a signal to the OS. This explains why your scripts aren't working on a long power button press.

To control the behavior, you need to configure how your operating system handles the ACPI shutdown signal. So if you want your computer to suspend for example, you would configure the OS to suspend on press of the power button.

But in the case of the long hold power button press, you do not get the opportunity to control anything because the OS doesn't receive a signal from the firmware.

If you follow the messages log

sudo tail -f /var/log/messages 

to see what logind logs when you press the power button. On my laptop (lenovo t14) and my desktop (asus motherboard), they both receive the signal after the power button is released.

Aug 16 11:47:43 tp-01 systemd-logind[1508]: Power key pressed.

This makes sense as the board's firmware is waiting to determine if the press was long or short. This also means that since a long press will immediately cut power to the system, the power will be cut before you release the button, and the signal will never be sent.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .