General TC > Programming & Scripting - Unofficial
battery-alert script
(1/1)
jpeters:
Script runs with cron. When battery is low, it generates a popup message and sound (if SOUND is set at top of script).
--- Code: ---#!/bin/ash
#
# bat-alert script, jpeters
#
# Script that works with cron to generate sound and popup message
# when battery is low.
#
# add "cron" to bootoptions
# Run with "crontab /bat-alert.cron"
# bat-alert.cron: "* * * * * export DISPLAY=:0 && apps/bat-alert"
# For Sound edit SOUND path:
#SOUND="/mnt/hda3/music/nature_birds.mp3"
MAXCAP=$(awk '/^last full capacity:/ {print $4}' /proc/acpi/battery/BAT0/info)
REMAINING=$(awk '/^remaining capacity:/ {print $3}' /proc/acpi/battery/BAT0/state)
CHARGING=$(awk '/^charging state:/ {print $3}' /proc/acpi/battery/BAT0/state)
let TEST=$REMAINING*5
### LOG : uncomment if interested ###########
#echo "CHARGING= $CHARGING" >>/tmp/bat.log
#echo "TEST= $TEST" >>/tmp//bat.log
#echo "MAXCAP= $MAXCAP" >>/tmp/bat.log
#echo "REMAINING= $REMAINING" >>/tmp/bat.log
#echo "" >>/tmp/bat.log
##### Alert if discharging and LOW
if [ $CHARGING == "discharging" ]; then
if [ $TEST -le $MAXCAP ]; then
aterm -e dialog --msgbox "Battery Low" 5 25 &
[ $SOUND ] && xmms $SOUND
fi
fi
--- End code ---
florian:
A cron job will do the job very well and in a flexible way, but FYI there might be even a simpler solution without cron. When the battery is getting low, ACPI should generate an "event" which you should be able to catch fairly easily. I'm not sure how to associate your own scripts to those events, I think you can set it up somehow in /etc/acpi (would need a bit of googling around...)
jpeters:
--- Quote from: florian on September 14, 2009, 05:32:30 PM ---A cron job will do the job very well and in a flexible way, but FYI there might be even a simpler solution without cron. When the battery is getting low, ACPI should generate an "event" which you should be able to catch fairly easily. I'm not sure how to associate your own scripts to those events, I think you can set it up somehow in /etc/acpi (would need a bit of googling around...)
--- End quote ---
I guess there's supposed to be an /etc/acpi/events, /etc/acpi/actions somewhere which also use a bash script. I saw the alarm & info settings in BAT0, but I'm not sure where the other files are. The script allows a little more time for closing things down (or reconnecting). I noticed that the drop-off at the end can be pretty fast.
Navigation
[0] Message Index
Go to full version