How to put a daemon on your laptop to hibernate when battery low ================================================================ [I don't use systemd, but I think it should work with it too.] Create /opt/battery/battery-watch containing: ------------------------------------------------------------------------------ #!/bin/bash while true; do cabled=`cat /sys/class/power_supply/ADP1/online` if [ $cabled -eq 0 ]; then status=`cat /sys/class/power_supply/BAT1/capacity` if [ $status -lt 10 ]; then swapoff -a swapon -a pm-hibernate fi fi sleep 60 done ------------------------------------------------------------------------------ The first test (the 'cabled' thing) is to check if the computer is plugged into the wall, in which case we don't care about the battery level and so we don't sleep if too low. You can adapt or remove it. Adapt the path of "capacity" to your own case. Adapt sleep time (here 60 seconds). Adapt charge below which you hibernate (here 10 for 10%). Create /etc/init.d/battery containing: ------------------------------------------------------------------------------ #!/bin/sh set -e BWATCH="/opt/battery/battery-watch" BWATCH_NAME="battery-watch" # Check for daemon presence [ -x "$BWATCH" ] || exit 0 # Get lsb functions . /lib/lsb/init-functions set +e case "$1" in start) log_daemon_msg "Starting battery-watch service" "battery-watch" start-stop-daemon --background --start --quiet --exec "$BWATCH" --name $BWATCH_NAME log_end_msg $? ;; stop) log_daemon_msg "Stopping battery-watch service" "battery-watch" start-stop-daemon --stop --quiet --retry 5 --name $BWATCH_NAME log_end_msg $? ;; restart) $0 stop sleep 1 $0 start ;; reload|force-reload) log_daemon_msg "Reloading battery-watch services" "battery-watch" start-stop-daemon --stop --signal 1 --exec "$BWATCH" log_end_msg $? ;; status) status_of_proc "$BWATCH_NAME" "$BWATCH" "system-wide $BWATCH_NAME" && exit 0 || exit $? ;; *) log_success_msg "Usage: /etc/init.d/battery {start|stop|restart|reload|force-reload|status}" exit 1 esac ------------------------------------------------------------------------------ Maybe not perfect. If you have a better program, share, thanks. Then run: update-rc.d battery defaults That will create links in /etc/rc*.d Here I have: ------------------------------------------------------------------------------ /home/sed> ls -al /etc/rc*/*battery* lrwxrwxrwx 1 root root 17 Jan 29 11:41 /etc/rc2.d/S03battery -> ../init.d/battery lrwxrwxrwx 1 root root 17 Jan 29 11:41 /etc/rc3.d/S03battery -> ../init.d/battery lrwxrwxrwx 1 root root 17 Jan 29 11:41 /etc/rc4.d/S03battery -> ../init.d/battery lrwxrwxrwx 1 root root 17 Jan 29 11:41 /etc/rc5.d/S03battery -> ../init.d/battery ------------------------------------------------------------------------------