Suspend laptop when lid is closed and power is removed

I changed my power settings on Ubuntu (although that is probably irrelevant for this topic) to sleep when the lid is closed when on battery but not to sleep when the lid is closed and the laptop is powered by AC.

Then, with the laptop on AC and the lid closed (i.e. running normally) I unplugged the AC and put the laptop into my laptop bag and went off to do some things in town.

An hour later I pulled my laptop out of the bag and the hot plastic smell was alarming – fortunately the laptop appears to be OK but this isn’t an episode I want to repeat. I figured that since Linux is quite configurable and is often driven by scripts that are run when certain events occur this issue should be preventable.

While I didn’t find a direct solution via Google I did know enough to eventually find what I wanted. I present this solution as a result. This method appears to work for Ubuntu 12.04 – it may work on other things as well. I didn’t have to install anything in particular to make this work but it does use the pm-utils (power management utils) stuff which may or may not be part of your distribution:

Also I’m not sure how robust the method of determining the lid state is (e.g. what if you have a LID0 and a LID1????). Someone said they wanted to see my laptop if it had a LID0 and a LID1 :)

Anyway, I created a file here : /usr/lib/pm-utils/power.d/suspend-when-ac-removed-while-lid-closed and made sure it was executable (i.e. chmod a+x /usr/lib/pm-utils/power.d/suspend-when-ac-removed-while-lid-closed)

Now for the script:

suspend() {
    # Check lidstate - open = 1, closed = 0
    LIDSTATE=`cat /proc/acpi/button/lid/LID0/state|grep open|wc -l`
    case "$LIDSTATE" in
        0) pm-suspend ;;
        *) echo Unknown $LIDSTATE ;;
case $1 in
    true) suspend ;;
    false) exit $NA ;;
    *) exit $NA ;;
exit 0

I didn’t need to restart pm-utils or any services, this just started working when I dropped the file in. To test:

  1. Configure Ubuntu to not suspend when you shut the lid and there is AC power.
  2. Shut the lid of your laptop (it should remain on).
  3. Remove the AC.
  4. The laptop should suspend.
  5. Open the lid and unlock the screen.
  6. Remove the execute flag (effectively disabling the script), chmod a-x /usr/lib/pm-utils/power.d/suspend-when-ac-removed-while-lid-closed
  7. Shut the lid and remove AC.
  8. The laptop should stay on.

In my testing over the last few days this appears to work all of the time – still after the heating episode I do check each time to see that it does sleep.

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

WordPress Themes