watch_battery

07 Apr 2011

I made a little script to look after my laptop battery so it doesn’t shutdown at the middle of me working. It requieres notify-send, hibernate and acpi, and targets Ubuntu:

$ sudo apt-get install acpi libnotify-bin hibernate

WARNING: For the hibernation to work the computer requires to have enough SWAP space (more than the amount of RAM)

The scripts analyze the battery status and send notifications if the charge is less than 15%,10% or 7%, if the equipment reaches 5% it sends a final warning and hibernate the machine.

I recommend to execute it every minute, a cron job can help:

*/1 * * * * /usr/local/bin/watch_battery

If you prefer to shutdown or suspend the equipment modify the $ACTION variable:

# Actions
ACTION="$(command -v hibernate)"

Make sure sudo can call the action command without requering password:

#===================================
# Cmnd alias specification
Cmnd_Alias SESSION=/usr/sbin/pm-suspend,/usr/sbin/hibernate,/sbin/shutdown

# usuario may use specific commands without passwd
user ALL=(root) NOPASSWD:SESSION
#===================================

Special thanks to smasty for the initial snippet.