WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: battery-alert script  (Read 4832 times)

Offline jpeters

  • Restricted
  • Hero Member
  • *****
  • Posts: 1017
battery-alert script
« on: September 14, 2009, 11:53:42 AM »
Script runs with cron.  When battery is low, it generates a popup message and sound (if SOUND is set at top of script).  

Code: [Select]
#!/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



« Last Edit: September 14, 2009, 12:24:51 PM by jpeters »

Offline florian

  • Full Member
  • ***
  • Posts: 116
    • Home Page
Re: battery-alert script
« Reply #1 on: September 14, 2009, 02: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...)

Offline jpeters

  • Restricted
  • Hero Member
  • *****
  • Posts: 1017
Re: battery-alert script
« Reply #2 on: September 14, 2009, 05:15:16 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...)

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.