You are not logged in.
Hi there,
I would like to set HandleLidSwitch depending on the AC state of my laptop.
If I run the laptop on battery, HandleLidSwitch should be set to suspend. If I am on charging, HandleLidSwitch should be set to ignore.
The HandleLidSwitch key is defined in /etc/systemd/login.conf. Thus I wrote a small script to detect changes of the AC state when the system is running, and change the HandleLidSwitch setting:
sed -i 's/HandleLidSwitch=ignore/HandleLidSwitch=ignore/' /etc/systemd/logind.conf
OR
sed -i 's/HandleLidSwitch=suspend/HandleLidSwitch=ignore/' /etc/systemd/logind.confHowever, the system does not consider my change in a running session because logind.conf is only considered once on user login but not during an already running session, correct?
Is there any way to overcome this shortcoming?
Thanks!
Last edited by orschiro (2014-06-12 12:54:41)
Offline
Where exactly did you put this script? How is it triggered?
Offline
@tomk
An udev rule detects the AC state change and runs the the script:
udev rule
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/bin/powersaving AC"script
#!/bin/bash
# Run powersaving script
case $1 in
battery)
echo "Running powersaving on battery"
# Enable suspend on lid closing
sed -i 's/HandleLidSwitch=ignore/HandleLidSwitch=suspend/' /etc/systemd/logind.conf
;;
AC)
echo "Running powersaving on AC"
# Disable suspend on lid closing
sed -i 's/HandleLidSwitch=suspend/HandleLidSwitch=ignore/' /etc/systemd/logind.conf
;;
esacOffline
I did more investitgation and found a command that might to the trick: systemd-inhibit
It provides a flag called --what=handle-lid-switch
However, I am struggling to understand how I could use this with a command that:
* changes the lid closing action to suspend
* is triggered only when the laptop is on battery. Otherwise the lid closing action should be none (default)
* is triggered only on lid closing
Basically I need something like:
systemd-inhibit --what=handle-lid-switch SOME_COMMAND_HEREOffline
The solution turned out to be very simple. I just have to restart the systemd-logind service after the sed command:
# Disable suspend on lid closing
sed -i 's/HandleLidSwitch=suspend/HandleLidSwitch=ignore/' /etc/systemd/logind.conf
systemctl restart systemd-logindOffline