I like to promote my suspend scripts to people. They work well
for me, and they have at least one feature that should be standard
in all suspend scripts, but for some reason isn't. When you close
the lid, it waits a short time (default 20 seconds) before actually
suspending. Therefore, if you change your mind, it isn't a big
deal to halt the suspend.
What you need
You need the Linux kernel's software-suspend functionality, so
make sure to compile that in. Also, when compiling your kernel,
don't forget to set your default resume partition
to your
swap partition. (Yes, you need a swap partition to suspend and
resume successfully. It probably doesn't work with swapfiles.)
I haven't tested this on any system other Debian. I know that
one person has made it work on Gentoo.
/etc/acpi/events/lidbtn
# /etc/acpi/events/lidbtn
# Called by acpid when the lid is closed, thus pressing the lid button.
event=button[ /]lid
action=/etc/acpi/lidbtn.sh
/etc/acpi/lidbtn.sh
Replace my username (duncan
) with yours as appropriate.
#! /bin/sh
if [ -e /var/lock/acpi-lid-switch ]
then
exit;
fi
# (Rather narrow) race condition exists, but it really doesn't matter
# to me now.
touch /var/lock/acpi-lid-switch
su duncan -c "~/bin/power.sh lid closed"
# How long a grace period do you want?
sleep 20;
if ( cat /proc/acpi/button/lid/LID/state | grep -q open )
then
rm -f /var/lock/acpi-lid-switch;
su duncan -c "~/bin/power.sh lid open"
exit;
fi
su duncan -c "~/bin/power.sh suspend"
# The ISC DHCP client doesn't work exactly as I want it to, so I kill
# it on suspend.
skill dhclient
# Make sure that USB filesystems are unmounted!
umount /mnt/bulla
echo disk > /sys/power/state;
# untold hours pass by ...
rm -f /var/lock/acpi-lid-switch;
su duncan -c "~/bin/power.sh resume"
~/bin/power.sh
This is to allow you to run whatever commands you want to on
power events. I'm sure that there's some sort of daemon to
implement this, but I haven't investigated. My way is simple and
works fine.
#!/bin/sh
# Script to react to power changes. Run as a normal user.
case $1 in
"lid")
case $2 in
"closed") ;; # do nothing
"open") ;; # cancel the actions from "closed"
esac ;;
"suspend") # we're about to suspend
xscreensaver-command -display :0.0 -lock ;;
"resume") ;; # the machine just came back from the dead
esac;
Notes
If you don't want the machine to suspend, regardless of the lid
switch state, just create the file /var/lock/acpi-lid-switch. In a normal Linux
setup, any user can create this file. The script will not remove
it. You have to do it yourself.