Description of problem:
Since Gnome no longer provides the possibility of inhibiting suspension when laptop's lid is closed (on AC power), I tried to do it myself. I want the system to suspend when on battery, but not suspend when on AC. Therefore, I added the following script in /etc/pm/power.d/01_lidclose.sh, so that it runs a systemd-inhibit command when using AC, and kills it when on battery. If I run the script manually, it works as expected and prevents suspends when the lid is closed. However, if I unplug AC and plug it again, the script is run by system (apparently, upower.service). also, systemd-inhibit shows that this inhibitor is running. So, it seems that everything is OK. However, if I close the lid, the laptop suspends.
The script (/etc/pm/power.d/01_lidclose.sh):
if on_ac_power; then
systemd-inhibit --what=handle-lid-switch --who="AC Lid-Close-Suspend Inhibitor" --why="Since AC power is available" --mode=block sleep 1000d > /dev/null &
echo $! > /run/lid_close.pid
kill -9 $(cat /run/lid_close.pid)
Output of 'journalctl -u systemd-logind.service -f' when the script is run manually:
ژانویه 27 22:24:11 hedayat-lap.hedayat-net systemd-logind: Lid closed.
ژانویه 27 22:24:17 hedayat-lap.hedayat-net systemd-logind: Lid opened.
When run automatically after unplugging and re-pluging AC:
ژانویه 27 22:24:34 hedayat-lap.hedayat-net systemd-logind: Lid closed.
ژانویه 27 22:24:34 hedayat-lap.hedayat-net systemd-logind: Suspending...
ژانویه 27 22:24:55 hedayat-lap.hedayat-net systemd-logind: Lid opened.
'systemd-inhibit --list' output when run in both cases:
[hedayat@hedayat-lap ~]% systemd-inhibit --list
WHAT WHO WHY MODE UID PID
handle-power-key:handle-suspend-key:handle-hibernate-key hedayat GNOME handlin...sses block 1000 1347
sleep hedayat GNOME needs t...reen delay 1000 1347
handle-lid-switch AC Lid-Close-...itor Since AC powe...able block 0 22461
3 inhibitors listed.
Version-Release number of selected component (if applicable):
Steps to Reproduce:
1. Put the above script in /etc/pm/power.d/
2. Make sure laptop is running on battery
3. Connect AC
4. Verify that the inhibitor is listed in 'systemd-inhibit --list' output
5. Close the lid
Should no suspend
If you run the script manually while laptop is on AC instead of steps 2 and 3, laptop doesn't suspend.
Other way how to set this directly in gnome is
gsettings set org.gnome.settings-daemon.plugins.power lid-close-ac-action blank
It doesn't work too! If I close the lid, laptop suspends. This is my status after running the command:
[hedayat@hedayat-lap ~]% gsettings get org.gnome.settings-daemon.plugins.power lid-close-battery-action
[hedayat@hedayat-lap ~]% gsettings get org.gnome.settings-daemon.plugins.power lid-close-ac-action
/etc/pm/power.d/ is not supported by logind's own suspend handling. Also, any hook directory like it is way too late to still inhibit the suspend.
Please file a bug against GNOME if the gsettings trick doesn't work.
Why /etc/pm/power.d/ should be supported by logind?! It is supported by upower, and it does run the scripts. (Notice that the hook is NOT intended to be run when you want to suspend, or wake up from suspend. The hook is run when the AC cable is plugged or unplugged. And it does run my systemd-inhibit command. So, the problem is this: systemd-inhibit is being run, and systemd-inhibit --list shows that an inhibitor is running, but it doesn't inhibit suspension.
Is it important who has run systemd-inhibit? Consider I write my own custom application to run systemd-inhibit when AC cable is plugged. Should it work or not? I guess it should work. So, why upower should not be able to inhibit suspension by running systemd-inhibit? Looks weird!
Note that the lid switch ignores any inhibtors by default, i.e. LidSwitchIgnoreInhibited=yes is the default. See logind.conf(5).
If the GNOME settings for controlling what happens if you close the lid don't work, then please file a bug against GNOME.