2

The setting HandleLidSwitch is defined in /etc/systemd/logind.conf as follows:

HandleLidSwitch=<hibernate,ignore,...>

The setting is applied successfully on boot. However, I would like to change the setting during run-time depending on the AC state:

If my laptop is charging, use HandleLidSwitch=ignore. If my laptop is on battery, use HandleLidSwitch=suspend.

Is this somehow possible to realise with current systemd?

1 Answer 1

5

logind provides an inhibitor lock mechanism that allows user software to override system policy. It allows, for example, Gnome to take control of all ACPI buttons and switches for as long as it is active. You could use it to inhibit "handle-lid-switch" when the device is plugged in, and drop the lock when it is unplugged.

The best approach would be to write a script in a language with DBus bindings and use logind's Inhibit() method, but systemd does provide a systemd-inhibit command that can be used from a shell script. Note that it is intended to be used as a wrapper, holding the lock until the child process exits:

systemd-inhibit --what=handle-lid-switch SOME_COMMAND_HERE

P.S. This all assumes you are not using a desktop environment that is already blocking "handle-lid-switch". Running systemd-inhibit without arguments will list existing inhibitors.

2
  • 1
    Thanks Chris. I run only Openbox window manager instead of a full DE. If I understood you correctly, it should be for instance # systemd-inhibit --what=handle-lid-switch suspend? I get this error: Failed to execute suspend: No such file or directory. Furthermore, # systemd-inhibit gives me 0 inhibitors listed.. Am I missing something?
    – orschiro
    May 7, 2014 at 21:23
  • The function is inhibited as long as SOME_COMMAND_HERE is running. For instance you could run systemd-inhibit --what=handle-lid-switch sleep 20 to block the configured action for a closed lid for 20 seconds. In your specific use case you would need to implement a command which starts as soon as AC is connected and is terminated if not.
    – doak
    Oct 2, 2021 at 19:04

You must log in to answer this question.

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