Battery check script

Hi all!  I'm sure this one has been done to death, but with.  It's a script to monitor a laptop battery and react accordingly.  I wrote this for use with FVWM, to send out warning notices using libnotify when the battery gets too low.  Since my laptop is quite old, the battery runs out quite quickly after getting to 25% capacity.  The script backgrounds itself on being run.  It starts when I begin an FVWM session, and I have FVWM kill the script when I logout.
Being a script it's easy to tweak to your particular requirements.  The example below just warns, but it would be easy to have the script suspend or hibernate the system, or change the percentage charge at which the script performs different actions.  I hope it's useful.
#!/bin/bash
# battery check script
# ====================
# Designed to be run continuously while WM is running
# Requires libnotify to be installed
ICONPATH=/home/rob/.fvwm/icons/24x18
BATDIR=/sys/class/power_supply/BAT0
NOTIFYSEND=/usr/bin/notify-send
if [ ! -x $NOTIFYSEND ]
then
exit 0
fi
# check every $CHECK seconds
CHECK=60
( while true
do
BATSTATE=`cat ${BATDIR}/status`
# Note valid BATSTATE values are: Charged, Charging, and Discharging
if [ "$BATSTATE" = "Discharging" ]
then
REM_CAP=`cat ${BATDIR}/charge_now`
FULL_CAP=`cat ${BATDIR}/charge_full`
CHARGE=`echo $(( $REM_CAP * 100 / $FULL_CAP ))`
if [ "$CHARGE" -lt "25" ]
then
$NOTIFYSEND -i $ICONPATH/battery_critical.png -u critical -t 30000 "battery critical" "Shutdown or connect power now"
sleep $CHECK
continue
fi
if [ "$CHARGE" -lt "30" ]
then
$NOTIFYSEND -i $ICONPATH/battery_low.png -u normal -t 20000 "battery low" "Connect power soon"
fi
sleep $CHECK
elif [ "$BATSTATE" = "Charged" ]
then
$NOTIFYSEND -i $ICONPATH/battery_full.png -u normal -t 20000 "battery fully charged" "Disconnect from power"
sleep $CHECK
else
sleep $CHECK
fi
done
# end of file
Edited 21/10/2011 to use /sys/class/power_supply/BAT0 instead of /proc/acpi/battery/BAT0
Last edited by Painless (2011-10-21 12:48:05)

I'm not exactly sure what you're trying to accomplish; your script seems to me to be prone to high cpu-useage loops. If all you want is a warning message if your battery is low when your adaptor is not plugged in, then you could try the following script.
One caveat, I don't have a laptop and I don't know what the output of
acpi -b
looks like, so I don't know which field contains the battery charge info. In the script below, I'm guessing that it is field number 3, so if it is different than that, the script below is incorrect and will not work.
If you don't understand what I am talking about, run
acpi -b
on your laptop, and post the output for me.
#!/bin/bash
trap "exit" SIGINT SIGTERM
delay=60
warning=50
while :
do
adapter=$(acpi -a | grep on)
b=$(acpi -b | cut -d ' ' -f 3)
[[ -z "$adapter" && "$b" -lt "$warning" ]] &&
notify-send "WARNING - LOW BATTERY" "Battery has ${b}% left."
sleep $delay
done
Last edited by rockin turtle (2011-01-15 06:08:45)

Similar Messages

  • My super simple ip checking script

    i have a small home network with a PC running arch that acts as a NAT router; in the near future i am planning on setting up VPN access on this arch box. i would assume that it is imperative to know the external IP address of the router in order to connect. my ISP is comcast (cable) and they seem to be rather active in changing my DHCP'd address i get from them (in the past 6 months i have had 3 different ones). so i wrote this bash script that runs in the background and monitors the IP of the external interface. its pretty self explanatory; the mails to "vtext.com" are so i can have it texted to me. pointers would be welcome. i am not particularly a programmer, much more just an admin. the awk stuff seems messy, like there should be an easier way to isolate strings than that.... but idk. i run it
    #!/bin/bash
    # IP CHECK 2.0 (file name/path: /etc/scripts/ipcheck)
    # runs in the background and
    # requires sleep, mailx & ifconfig
    # assumes external interface named "eth0"
    # current ip output file path - single-line file containing current address (needs to be writable)
    OPATH="/var/ipcheck/currentip"
    # Amount of seconds to wait between checks
    WAITSECS=300 # 5 mins
    # Read previous IP from specified $OPATH file, if it's there
    if [ -f "$OPATH" ]; then
    OLDIP=`cat $OPATH`;
    fi
    # Begin main program loop
    while :
    do
    # retrieve new ip using ifconfig
    NEWIP=`/sbin/ifconfig eth0 | awk 'BEGIN{FS="inet ";RS=" netmask"}/</{print $2}' | tr -d '\\n' | tr -d ' '`
    # print whatever was awk'd and the current date/time to tty11
    echo "========= $NEWIP -- `date`" >> /dev/tty11
    # if a different NEW IP is detected AND it isn't a null value, then do the following:
    # * write the new address out to the OPATH file, replacing the previous value
    # * e-mail the new address to my phone via vtext.com
    # * e-mail both the old and new addresses to my main e-mail account
    # * reset the $OLDIP value as the new one
    if [[ "$OLDIP" != "$NEWIP" && "$NEWIP" != "" ]]; then
    echo $NEWIP > $OPATH
    echo "======= New Address - Old IP: $OLDIP ---- New IP: $NEWIP" >> /dev/tty12
    echo "New IP: $NEWIP" | /usr/bin/mailx [email protected]
    echo "Old IP: $OLDIP" | /usr/bin/mailx -s "New IP: $NEWIP" [email protected]
    OLDIP=$NEWIP
    fi
    # pause in between checks, then start again
    sleep $WAITSECS
    done

    falconindy wrote:
    Isola wrote:
    Here's my IP checking script, modified for mailx
    #!/bin/bash
    curl ifconfig.me | read IP
    [ "x$IP" == "x`tail -n1 /tmp/wanip`" ] && exit 0
    echo New IP: $IP
    echo -n Old IP:
    cat /tmp/wanip
    echo -n Number of times you had this IP before:
    grep $IP /tmp/wanip | wc -l
    } | mailx -s "New IP: $IP" [email protected] [email protected]
    echo $IP >> /tmp/wanip
    This can't possibly work as is... The assignment in the first line happens in a subshell. It's going to be blank in the rest of the script.
    Okay I admit, I just wrote that without testing it
    edit:
    The 2nd line can be changed for this:
    read IP < <(curl ifconfig.me) || exit $?
    Or
    IP=$(curl ifconfig.me) || exit $?
    Last edited by Isola (2012-07-01 00:59:39)

  • How to use dreamweaver to check scripts.

    I am both new to scripting and in using dreamweaver, I n
    eed to be able to copy an asp code and paste in dreamweaver and c
    ontinue with it, can anyone please help me on how to do this? , I a
    m testing dream weaver cs4

    I don't understand your subject line, "How to use dreamweaver to check scripts".  Can you explain what you mean by this?
    >I need to be able to copy an asp code and paste in dreamweaver and
    >continue with it, can anyone please help me on how to do this?
    You just copy the code from one page and paste into another. If you are having problems, you need to be more specific about what is not working.

  • Health check script suggestions...

    I used the health check script from metalink and it gave me few suggestion regarding my replication environment. One of them is ...
    + SUGGESTION: One or more propagation processes contain rule sets.
    + If a Propagation process will unconditionally forward all incoming
    + messages to its destination queue, and no rule-based transformations are
    + performed by the Propagation process, you should consider removing
    + the rule set for the Propagation process via dbms_propagation_adm.alter_propagation.
    + This will improve Propagation performance.
    What is get from this is since i have same rule defined in propagation also as the one in capture, So suggestion from orcale is remove the propagation rule and let everything propagted through...anyhow since i want everything a capture process captures to be replicated on destenation.. so i removed the propagation rule. ..
    But now the replication has stoped performing after that.....
    can any one suggest me if my undersanding for above healthcheck suggestion is wrong then wht does it mean?
    Kapil

    Did you remove both the rule AND the rule set, or did you remove only the rule?
    If it was a positive rule set and you removed only the rule, you left an empty rule set as a positive rule set for the propagation. This will cause it to discard all messages.
    An empty positive rule set is not the same as no (null) positive rule set. A null rule set means "forward all messages, since there is not filter rule", but an empty rule set means "there is a rule: that no message will be forwarded".
    Ilidio.

  • How to do the battery check

    hello
    i have problems to do the battery check with the Elitebool 2540p. it says that the battery need to be collocated properly and connect the computer to the AC, which is done.
    i do not know what is happening, and need to calibrate the battery, since in a week the performarce decreased abruptly, although with no suspicious behaviour.
    thank you

    Hi,
    See the link below regarding manual callibration of your notebook battery if running Vista or Windows 7.
    Callibrating The Battery.
    You could also download and run the stand-alone battery test utility available on the page below and see if this reports any problems with your battery.
    http://h20239.www2.hp.com/techcenter/battery/batte​ry_ts.htm
    Regards,
    DP-K
    ****Click the White thumb to say thanks****
    ****Please mark Accept As Solution if it solves your problem****
    ****I don't work for HP****
    Microsoft MVP - Windows Experience

  • DB Health Check Scripts

    Hi all,
    Oracle 9i, 10g, 11g
    Can you share me your "health check" scripts which you have used in your PROD database and is proven to help you in carrying out your desired tasks being a good DBA :)
    Does AWR be able to generate health check report?
    Thanks a lot,
    Kinz

    KinsaKaUy? wrote:
    Thanks sb,
    http://www.orafaq.com/wiki/Scripts
    Wow this one site is complete, But it will take years for me to read all these. And I dont know if this is for 8i, 9i, 10g, or 11g.
    Can you tell me which of these scripts will list the Top 10 most resource consumers queries? in terms (CPU,Memory,I/O).If you enable SQL_TRACE prior to launching AWR report, then you capture EVERY SQL statement to build the report.
    You could easily observe the answer to your question if you were only willing to actually did some lilttle work instead of asking to be spoon fed.

  • Pre-requisite checker script for EBS R12 and R12.1

    hi
    is it possible to make a pre-requisite checker script (in perl or shell) for EBS R12 and R12.1 in RHEL5 or sun OS??
    can anyone help / guide me to do so??
    rgrds

    I don't know about EBS R12, but apparently the Oracle Universal Installer can use options to just run the prerequisite checks without having to install the product.
    E.g. /runInstaller -prereqChecker or -executeSysPrereqs
    There is also a specific OTN forum available for installing E-Business suite:
    LCM: 11i Install/Upgrade

  • I run Battery Check but I get a conflicting error message

    I run the Battery Check and it tells me the battery is functioning correctly but that it cannot complete the test if the AC adapter is not attached, but it is and is charging the battery?
    HP Pavilion dv7 Notebook PC, win7 64 bit

    Do a clean reinstall and download a fresh Firefox copy from http://www.mozilla.com/firefox/all.html and save the file to the desktop.
    Uninstall your current Firefox version and remove the Firefox program folder before installing that copy of the Firefox installer.
    It is important to delete the Firefox program folder to remove all the files and make sure that there are no problems with files that were leftover after uninstalling.
    See http://kb.mozillazine.org/Standard_diagnostic_-_Firefox#Clean_reinstall

  • Check(Script) - Pageformat

    Hi friends,
    I need the pageformat to assign as exactly as to my Check script.Here,Im using only the check window as needed to print the check details same to my client bank cheque.So, as u aware, the height of the check size will be upto approx 8cm and my client are using the dot matrix serial print cheques(continuous one as rolling format).
    Just reply me the exact pageformat.
    ****I've tested with available std INCH4/4C/2 and scheck formats but got no results.
    Pls advise.
    thanks & regards
    sankar.

    Hi,
    Solution for this problem will solve by your client, ask cleary the what the pageformat they are using. and by trail and error basis set the window heights, and Format the line, Paragrah, characer format.
    Bye

  • SAP Bundle Patches and Automatic Oracle parameter check script (note117165)

    After reading SAP note 1027012 on SAP Bundle Patches and SAP note 1171650 on the
    automatic Oracle parameter check script I have a simple question:
    Is it still necessary to manually check whether some patch is installed in order to determine
    exactly which event and fixcontrol to set?
    Or is the automatic Oracle parameter check script so sophisticated that it is fully
    sufficient to implement its recommendations, and you don't neet to manually
    check any more which SBP with which bugfixes has been installed?
    It would be great if SAP could confirm that the automatic parameter check is sufficient,
    but so far I haven't seen this statement explicitly. I believe this is mostly because it
    is so difficult (impossible?) to determine via SQL which patches have been applied.
    Regards,
    Mark

    >
    > Is it still necessary to manually check whether some patch is installed in order to determine
    > exactly which event and fixcontrol to set?
    Yes and No.
    until now, fixcontrol are dependent of CBO patches and those are "register" on the view v$system_fix_control. They can be "check" autmatically, so you get the "proper" recommendation for those.
    the parameter event depends on different situations, like some patches. But those patches are "normal" patches and it is not possible to check if they are installed from inside the DB with a simple script. For those, the check script will tell you to do it manually and will tell you which patch has to be check
    In addition, there are other parameters that have to be check "manually", like processes (where you get a "semiautomatic" check) or db_cache_size, where you should check if the size
    > It would be great if SAP could confirm that the automatic parameter check is sufficient,
    > but so far I haven't seen this statement explicitly. I believe this is mostly because it
    > is so difficult (impossible?) to determine via SQL which patches have been applied.
    The automatic parameter check is sufficient, meaning that you get all information, including the parameters that you have to check manually and some information for that check. Currently, it is not possible to automatize it 100%

  • Bash Server Ping Checking Script

    Posted this on my blog yesterday, but thought i'd share it here too.
    I had alot of fun writing this little ping testing utility. It works/looks best if you have a color enabled terminal.
    Could be easily modified to email you if the server failed. Anyway, just thought I'd share
    #!/bin/bash
    # Bash Server Ping Checking Script
    # 09-24-2009 Crouse
    # Pretty simple, add servers/ip's/Ln (line numbers) in the format shown in the two column examples.
    # Tested with over 50 servers, worked for what I needed, thought i'd share :) ~~Crouse.
    # Ideas --- ad another array for Headers. so Hn could print headers with bold/etc. Might be neat.
    declare -a Sn # Server Name
    declare -a Ip # IP address of server.
    declare -a Ln # Line number to display on in Row1.
    declare -a Sn2 # Server Name
    declare -a Ip2 # IP address of server.
    declare -a Ln2 # Line number to display on in Row2.
    ############################################################################### Server List Row 1
    Sn[1]="usalug.org"
    Ip[1]="67.15.6.98"
    Ln[1]="6"
    Sn[2]="archlinux.me"
    Ip[2]="208.92.232.122"
    Ln[2]="7"
    ############################################################################### Server List Row 2
    Sn2[1]="usalug.org"
    Ip2[1]="67.15.6.98"
    Ln2[1]="6"
    Sn2[2]="archlinux.me"
    Ip2[2]="208.92.232.122"
    Ln2[2]="7"
    tput civis # Hide the cursor
    maxcount=${#Sn[*]} #should report number of Servers listed in Sn variable
    maxcount2=${#Sn2[*]} #should report number of Servers listed in Sn2 varialbe
    clear
    #tput setf 2; #echo -e '\e[1;33m'
    echo " "
    tput setf 0;tput setb 2;
    echo " Bash Server Ping Checking Utility version 2.0 Crouse. "; #tput rmso;
    tput setf 7; tput setb 0; # List out servers now.
    for (( loop=1; $loop < $maxcount+1; loop++ ))
    do
    ( tput cup ${Ln[$loop]} 10; echo "${Sn[$loop]}"; tput rc)
    done
    for (( loop2=1; $loop2 < $maxcount2+1; loop2++ ))
    do
    ( tput cup ${Ln2[$loop2]} 50; echo "${Sn[$loop2]}"; tput rc)
    done
    # Set traps so when we exit the big loop stuff is normal again.
    gracefulexit () {
    tput cnorm;
    tput setb 0;
    echo "Stopping Application"
    tput setf 2;
    exit
    trap gracefulexit INT TERM EXIT
    # Begin big loop
    while true
    do
    #First Row
    for (( loop=1; $loop < $maxcount+1; loop++ ))
    do
    (tput sc ;tput setb 0; tput setf 0; tput cup 4 10 ;
    echo "........................................................................";tput rc)
    (tput sc ;tput setb 0; tput setf 6; tput cup 4 10 ;
    echo "Checking ${Sn[$loop]} at IP: ${Ip[$loop]}" ; tput rc)
    (tput sc ;tput setb 0; tput setf 0; tput cup ${Ln[$loop]} 1 ; echo "........." ; tput rc)
    (tput sc ;tput setb 0; tput setf 6; tput cup ${Ln[$loop]} 1 ; echo "TESTING" ; tput rc)
    count=$(ping -c 5 ${Ip[$loop]} | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
    (tput sc ;tput setb 0; tput setf 0; tput cup ${Ln[$loop]} 1 ; echo "........." ; tput rc)
    if [[ "$count" == [45] ]]; then
    (tput sc ;tput setf 0; tput setb 2;tput cup ${Ln[$loop]} 5 ; echo " OK " ; tput rc)
    else
    if [[ "$count" == [123] ]]; then
    (tput sc ;tput setf 0; tput setb 6; tput cup ${Ln[$loop]} 4 ; echo "ALERT" ; tput rc)
    else
    (tput sc ;tput setf 7; tput setb 4; tput cup ${Ln[$loop]} 2 ; echo "WARNING" ; tput rc)
    fi
    fi
    done
    #Second Row
    for (( loop2=1; $loop2 < $maxcount2+1; loop2++ ))
    do
    (tput sc ;tput setb 0; tput setf 0; tput cup 4 10 ;
    echo "........................................................................";tput rc)
    (tput sc ;tput setb 0; tput setf 6; tput cup 4 10 ;
    echo "Checking ${Sn2[$loop2]} at IP: ${Ip2[$loop2]}" ; tput rc)
    (tput sc ;tput setb 0; tput setf 0; tput cup ${Ln2[$loop2]} 41 ; echo "........." ; tput rc)
    (tput sc ;tput setb 0; tput setf 6; tput cup ${Ln2[$loop2]} 41 ; echo "TESTING" ; tput rc)
    count2=$(ping -c 5 ${Ip2[$loop2]} | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
    (tput sc ;tput setb 0; tput setf 0; tput cup ${Ln2[$loop2]} 41 ; echo "........." ; tput rc)
    if [[ "$count2" == [45] ]]; then
    (tput sc ;tput setf 0; tput setb 2;tput cup ${Ln2[$loop2]} 45 ; echo " OK " ; tput rc)
    else
    if [[ "$count2" == [123] ]]; then
    (tput sc ;tput setf 0; tput setb 6; tput cup ${Ln2[$loop2]} 44 ; echo "ALERT" ; tput rc)
    else
    (tput sc ;tput setf 7; tput setb 4; tput cup ${Ln2[$loop2]} 42 ; echo "WARNING" ; tput rc)
    fi
    fi
    done
    done
    Last edited by crouse (2009-09-25 14:40:42)

    http://social.technet.microsoft.com/Forums/exchange/en-US/6b8d299d-eed9-4493-bd49-42399f478f93/exchange-2010-server-up-time-script-help-needed?forum=exchange2010

  • Is there any upgrade checking script for PL upgrade

    Dear Experts,
    I know there are Pre-upgrade checking script to validate database integrity before an upgrade from 2005 to 2007.
    However, is there any such similar scripts for an upgrade from 2007 PL12 to PL13? (Inter PL upgrade) or can I use the same script for the same purpose?
    Much Thanks!
    Warmest Regards,
    Chinho

    Hi Gordon,
    Thank you for your reply. Is it true that within inter-patch upgrades, there are no database structure change?
    I asked this question because sometimes I get "DB is inconsistent" error after upgrade, and I don't know why?
    Much Thanks for your Advice thus far!
    Warmest Regards,
    Chinho

  • Battery check compaq presario cq56

    is safe to run a laptop just withe a/c after the battery is charged, and if is posible repair the Hp assistant aplication is not working properly?? are any others programs to check is the battery status   Gracias

    Hello,
    I see that you're having an issue with HPSA not working properly. Now, I'm not aware of any other 3rd party program that gives you the ability to check your battery’s performance. 
    Now, to resolve the HPSA issue you have going on. You will need to uninstall the program through the Add/Remove folder in the control panel. After that, you will need to install this version of HPSA. Once you install that you should be able to use the battery check within the program. 
    If the issue still persists please let me know. If you have any more questions let me know.
    THX

  • HP Battery Check Shows Status 80

    Can Anyone Please Tell Me The Meaning of Status: 80 (Circled With Red)

    Hello @HPUSER1102 ,
    Welcome to the HP Forums!
    Status 80 of the advanced HP Battery Check indicates a perfectly healthy battery! Congratulations
    Mario
    I worked on behalf of HP.

  • HP Battery Check Status: replace (41)

    Hello Guys.
    I have an issue with my laptop battery. Laptop shuts down immediately after power chord is removed and wont boot until I reconnect it.
    I have been using this battery for 4 months and it's 5200mAh 8cell battery. Windows says that battery is 100% charged
    Hp Battery Check status is replace (41)
    help?
    I'm using a HP Probook 4510s with windows 8 OS.
    Thanks in advance.

    Sounds like your battery is not functioning properly. I would first run a battery calibration on your laptop. ProBook Battery Calibration. If this does not solve the issue then most likely the battery needs replaced. Are you still in warranty?
    Thanks
    Sean
    -------------How do I give Kudos? | How do I mark a post as Solved? --------------------------------------------------------

Maybe you are looking for

  • Issue in Directly Making payment  from bank for the exp. blong to 2 PC

    Dear All, I have one scenario in which i have to make one payment for the  expenses directly and the expenses belong to 2 different Profit center and document splitting is activated at profit center level. Example Traveling Exp Ac Dr       100       

  • Is iPhoto the best program to import 30000 pictures?

    Hi There, I have over 30 K pictures sitting on a 3 TB external HD. Many duplicates and many I want to delete. What is the best way to review, catalog and after trimmed, save on a SSD external drive, since my macbook Air have only 250 GB space total.

  • Zone Paging

    Hi, Please send document to develop coding for the following Zone Paging in Cisco IP Phone Audio Text Text-to-Speech Analog Overhead Paging System Integration Live Audio Broadcast Pre-recorded-Audio/Text/TTS Pre-scheduled-Audio/Text/TTS Short email f

  • First Name first in composing mail

    in my address book, I sort by the last name. When I want to compose an email, the name comes up with the last name first and then the first name (like the address book). I would like the first name first in my emails. How do I change this.

  • Log & Transfer of .R3D to Apple Pro Res HQ stopped working

    I had been successfully Transferring my .R3d footage to Apple Pro Res HQ and now that I'm about half way through I am unable to bring my footage into the Log & Transfer queue. When I select the appropriate folder via Directory and click Open, nothing