Auto-suspend when ACPI Lid State: closed


My new toshiba (tecra a5) laptop (with Ubuntu Linux installed) became jealous of these windows laptops that can sleep when then monitor/lid is closed. So I decided to add that ability trying to save myself some extra battery power when I can during classes.

Some current caveats include: even if the laptop is busy it will suspend, its just a bash script making me feel like its the old days before the kernel had cpu throttling

I wrote this rc script to start it up... (/etc/init.d/acpi-lid-monitor)
#!/bin/bash
#acpi monitor - heavy weight monitor

. /lib/lsb/init-functions
. /etc/default/rcS

case "$1" in
  start)
    log_begin_msg "Start LID Monitor..."
    start-stop-daemon --start --quiet --make-pidfile --pidfile /var/run/lid-monitor.pid --exec /home/woodzy/scripts/lid-monitor.bash &
    log_end_msg $?
  ;;
  stop)
    log_begin_msg "Stopping LID Monitor..."
    kill `cat /var/run/lid-monitor.pid`
    rm -rf /var/run/lid-monitor.pid
    log_end_msg $?
    ;;
  *)
    log_success_msg "Usage: /etc/init.d/acpi-lid-monitor {start|stop}"
    exit 1
    ;;
esac

exit 0


The actual script is as follows... (scripts/lid-monitor.bash)
#!/bin/bash
#acpi monitor - heavy weight monitor

count=0

while true; do
  grep -q off-line /proc/acpi/ac_adapter/*/state
  ac_adap=$?
  grep -q closed /proc/acpi/button/lid/*/state
  lid_btn=$?

  if (( ($lid_btn == 0) && ($ac_adap == 0) ));
  then
    $((count++))
  else
    count=0
  fi

  if [ $count = 3 ]
  then
    /etc/acpi/sleep.sh
  fi

  sleep 2m
done


Of course you'll need to add these to the default run levels so it will start. At some point I'll add activity checking or something like that... and change the daemon running to a C daemon so its a little lighter.
But for right now I'm satisfied with just the ac_adapter checking to see whether the lid closed will allow this script to send the laptop into suspend mode.

-Woodzy
(matt /at/ woodzy.com)
Last Modified: Dec 31, 2005