Simple script to get battery charge and time remaining...

Here's something I threw together last night, to report on the battery at the command line.  It uses /proc/acpi/battery.  My battery is C11F, just substitude your own.
It takes (remaining capacity) / (design capacity) to get the percent left, and it takes the (discharge rate) / (remaining capacity) to get approximate life.  If the rate is zero (which mine reports as when plugged in) it says the battery is charging.
I don't know if this will work for other people, or if different batteries give different times of information to ACPI.
#!/bin/bash
current=`grep remaining /proc/acpi/battery/C11F/state | tr -d A-z | tr -d " " | tr -d :`
total=`grep "design capacity:" /proc/acpi/battery/C11F/info | tr -d A-z | tr -d " " | tr -d :`
rate=`grep "present rate:" /proc/acpi/battery/C11F/state | tr -d A-z | tr -d " " | tr -d :`
percent=`bc << EOF
100.0 * $current / $total
EOF
`
if [ $rate -eq 0 ]; then
echo $percent'%, charging'
else
hours=`bc << EOF
(1.0*$current) / (1.0*$rate)
EOF
`
minutes=`bc << EOF
60.0 * $current / $rate - $hours * 60
EOF
`
echo $percent'%, '$hours':'$minutes
fi

zen_guerrilla wrote:You can also try the 'acpi' pkg, which does exactly the same thing .
Considering the acpi package is only dated a few days ago, I don't feel too bad about not knowing about it.
BTW, here's an update I did to my script.  This one prints minutes less than 10 correctly (1:02 vs 1:2), as well as finding batteries with other names, and printing the CPU speed (nice if you have speed stepping turned on).  It rounds to the nearest 100MHz, which is easy to remove if you don't like it.
#!/bin/bash
speed=`grep "cpu MHz" /proc/cpuinfo | tr -d A-z | tr -d " " | tr -d :`
current=`grep remaining /proc/acpi/battery/*/state | tr -d A-z | tr -d " " | tr -d :`
total=`grep "design capacity:" /proc/acpi/battery/*/info | tr -d A-z | tr -d " " | tr -d :`
rate=`grep "present rate:" /proc/acpi/battery/*/state | tr -d A-z | tr -d " " | tr -d :`
int_speed=`bc << EOF
($speed+50) / 100 * 100
EOF
`
percent=`bc << EOF
100.0 * $current / $total
EOF
`
if [ $rate -eq 0 ]; then
echo $int_speed'MHz, '$percent'%, charging'
else
hours=`bc << EOF
(1.0*$current) / (1.0*$rate)
EOF
`
minutes=`bc << EOF
60.0 * $current / $rate - $hours * 60
EOF
`
if [ $minutes -lt 10 ]; then
echo $int_speed'MHz, '$percent'%, '$hours':0'$minutes
else
echo $int_speed'MHz, '$percent'%, '$hours':'$minutes
fi
fi

Similar Messages

  • I want to know what happens when the iPad battery gets fully charged and the charger is still connected

    When the battery gets fully charged, and the charger is still connected to the iPad, does the iPad use power from the battery or does it use the charger?

    Install Battery Doctor and see Battery Status or Charging
    http://itunes.apple.com/tw/app/battery-doctor-hd/id459702901?mt=8
    System Status will indicate charged when charging circuit is disabled
    http://itunes.apple.com/sg/app/system-status-activity-manager/id401457165?mt=8&l s=1

  • How do I get battery charge % to display on iPhone 3G?

    How do I get battery charge % to display on iPhone 3G?

    Hi,
    I have an iPhone 3G too, and actually you can't see the % battery life in your display. Try to download an application that I have, it's called Battery and you can see you battery life of your iPhone. It's pretty good;)

  • Get the date and time from iframe.

    Hi,
    Am using jdeveloper 11.1.1.6.0., I have used the following iframe source which gets the date and time.
    <af:form>
    <iframe src="http://free.timeanddate.com/clock/i3xb4nm0/n1722/tt0/tw0/ts1" frameborder="0" width="244" height="19">
          </iframe>
    </af:form>
    I need to get the date and time separately to perform some validation. How can i  get it.
    regards,
    Prasad K T,
    9840021070

    Prasad,
    I don't understand your use case. The url you use is a json web service which returns it's result as either xml or json payload. Why do you pretty print this as a web page and try to get to the result this way?
    Why don't you get the result directly using the json result or (AFAIK) the java script api which the service also provides.  Check the documentation of the service.
    From your description I understand that the user enters a time. You need to know if the time is before or after 2:30pm and have to set a 'shift' to A or B.
    Where do I need a timeservice in this validation?
    From what I see your approach won't work. As I still don't understand the use case I don't know how to help.
    Timo

  • In mail, after importing mailboxes the process gets stuck on '1619 of 72826' and 'time remaining' climbs infinitely. Please suggest a solution.

    In 'mail', after importing mailboxes the process gets stuck on '1619 of 72826' and 'time remaining' climbs infinitely. Please suggest a solution.
    I converted my mail boxes from POP to imap for the purpose of syncing all my devices. I backed up my mail boxes and further more perfomed a timemachine backup.
    Upon restoring ther mailboxes, either by manually copying them to the mailboxes folder, and restoring a timemachine backup, both pocesses result in the above problem.
    Please recomend a solution.
    file://localhost/Users/redwood/Desktop/Screen%20Shot%202013-10-15%20at%201.48.10 %20PM.png
    file://localhost/Users/redwood/Desktop/Screen%20Shot%202013-10-15%20at%201.38.30 %20PM.png

    Can anyone out there please help!

  • Problem with getting current date and time using oracle.jbo.domain.Date

    I`d like to get current date and time using oracle.jbo.domain.Date method getCurrentDate(), but it always return current date and 12:00:00. I also need to get the current time.

    I think you should use java.sql.Timestamp domain.
    (And set database type to TIME or DATETIME.)
    Jan

  • What happened to the Get/Set Date and Time utilities from RT 7.1?

    I'd like to use the Get/Set Date and Time utilities that were available in 7.1, but disappeared after that. I see that the 7.1 utilites  call a dll that does not appear to be installed with LabVIEW RT 8 and later. What do I need to do to use the utilites from 7.1 that do not appear in later versions?

    In Labview 8.2 and later there are two directories for vi.lib. settime.dll is located in National instruments\Labview 8.2\Targets\NI\RT\ vi.lib
    This is the dll you want to use in order to call it use the same Vi as in 7.1 (RT set date and time.vi) and link it to the correct dll.

  • How to get exact date and time difference?

    Hi,
    When i am using the below sql statement:
    SELECT (TO_DATE('7/27/2006 05:00:15 PM','MM/DD/YYYY HH:MI:SS PM') - to_Date('7/27/2006 8:30:13 AM','MM/DD/YYYY HH:MI:SS AM')) * 24 hours FROM DUAL;
    the output is:
    Hours
    8.50055555555556
    But Actually it is 8.30
    So how can i get exact days and time difference between two dates in Oracle?
    Thanks....
    Regards,
    Suman Sakinala
    Edited by: SSN on Sep 19, 2008 1:27 AM

    Or more clearly (this time I have my date_from and date_to the right way around hehe!)
    SQL> ed
    Wrote file afiedt.buf
      1  WITH t as (select TO_DATE('7/27/2006 05:00:15 PM','MM/DD/YYYY HH:MI:SS PM') as dt_to,
      2                    TO_DATE('7/27/2006 08:30:13 AM','MM/DD/YYYY HH:MI:SS AM') as dt_from FROM DUAL)
      3      ,d as (select (dt_to-dt_from)*24 as diff_hrs from t)
      4  -- end of dates
      5  SELECT TRUNC(diff_hrs) as hours
      6        ,TRUNC(((diff_hrs) - TRUNC(diff_hrs))*60) as mins
      7        ,TRUNC(((((diff_hrs) - TRUNC(diff_hrs))*60) - TRUNC(((diff_hrs) - TRUNC(diff_hrs))*60))*60) as secs
      8* FROM d
    SQL> /
         HOURS       MINS       SECS
             8         30          2
    SQL>

  • Get  current date and time

    hello.
    How can I get current date and time in JAVA?
    Thanks.

    Sorry, I mean:
    how can I get the value of DATE, MONTH, and YEAR of the
    current date?
         Date date = new Date();
         System.out.println(date);

  • How can I get the date and time and display it on the report main page?

    Gurus,
    How can I get the date and time and display it on the report main page?
    Thanks!

    Hello,
    You can create a Formula Colum returning a date :
    function CF_1Formula return Date is
    begin
    RETURN(SYSDATE);
    end;
    Put a Field in the Layout having this formula column as source .
    Regards

  • Getting the date and time.

    What is the best way to get the date and time?
    Thanks

    What is the best way to get the date and time?
    ThanksI am not sure that this is the best way. But one of the ways.
    <code>
    java.util.Date d = new java.util.Date();
    System.out.println("date is "+d.toString());
    </code>

  • How to get system date and time?

    Can someone show me a code on how to get system date and time.
    Thanks!

    there is one really easy way to get system time, the api gives a great example of code on this. use gregorian calendar, which you'll find in the api under GregorianCalendar. You only need to create one instance of GC, ie Calendar time = new GregorianCalendar();
    you save seconds, minute and hours into int values, so you don't have to access the system time every second, you can create a thread which adds one to the int second value, if oyu see what i mean, for example, i have saved the hours, minutes and seconds as int values;
    int hour, minute, second;
    i can then create a thread (Thread thread = new Thread(this) and run it like:
    Calendar time;
    int hour, minute, second;
    Thread thread = null;
    public MyTime() {
    hour= time.get(Calendar.HOUR_OF_DAY);
    minute = time.get(Calendar.MINUTE);
    second = time.get(Calendar.SECOND);
    if(thread == null) {
    thread = new Thread(this);
    thread.start();
    public void run() {
    Thread t = Thread.currentThread();
    while(thread == t) {
    thread.sleep(1000);
    second++;
    if(second > 59)
    minute++;
    if(minute>59)
    hour++;
    formatTime();
    public void formatTime() {
    second = (second > 59? 0 : second);
    minute = (minute > 59? 0 : minute);
    hour = (hour > 23? 0 : hour);
    System.out.println(hour+":"+minute+":"+second);
    public static void main(String[] args) {
    new MyTime();
    I know this looks like gibberish but it should work. If not, try to fix the problem, i have written from memory really but i guarantee you, this gets the time then every second, simply adds one to the second and then formats time. You can also access the day, month and year then format them using the above code. I don't like giving code since you should really do these things yourself but it is 2:04am, i have nothing better to do and i am not tired so i did you a favour - i have become what i always did not want to, someone ho stays upall night writing code.

  • Getting the date and time from NIST server

    I need to be able to get the date and time in an app and cannot trust the device's clock since the user could change it. Has anyone got an example of how to access an Internet time server such as the NIST servers from AIR for iOS?

    What is the best way to get the date and time?
    ThanksI am not sure that this is the best way. But one of the ways.
    <code>
    java.util.Date d = new java.util.Date();
    System.out.println("date is "+d.toString());
    </code>

  • An app was purchased on my ipad by accident, can I get the charge and app removed?

    An app was purchased on my ipad by accident, can I get the charges and app removed?

    Hi,
    Sorry to say, all sales are final.
    iTunes Store: All Sales Are Final
    "You cannot cancel a purchase or receive a refund for a purchase. See the iTunes Terms of Sale for additional information."
    Carolyn

  • HT1414 My iPad Air, 32 gb will not start up. It had 97% battery charge, last time I used it. I tried connecting to my iMac but iTunes said I needed to add my password to my iPad Air, which I can't do since the iPad Air won't start up. The iPad air is one

    My iPad Air, 32 gb will not start up. It had 97% battery charge,last time I used it. I tried connecting to my iMac but iTunes said I needed to add my password to my iPad Air, which I can't do since the iPad. Air won't start up. The iPad air is one week old. Bummer, eh?

    Auld Dave wrote:
    . I tried connecting to my iMac but iTunes said I needed to add my password to my iPad Air, which I can't do since the iPad. Air won't start up.
    See Here  >  http://support.apple.com/kb/HT1808
    You may need to try this More than Once...  Be sure to Follow ALL the Steps...
    After you have Recovered your Device...
    Re-Sync your Content or Restore from the most recent Backup...
    Restore from Backup  >  http://support.apple.com/kb/ht1766

Maybe you are looking for

  • Can I change dates underneath events?

    I am brand new to iphoto and have a question. There is a date, and sometimes a range of dates, that show up underneath each event when the mouse is placed over the event or the event is active. For some of my events, this date is accurate. For others

  • Which anti-virus program do you recommend?

    My school requires that I install an anti-virus on my iBook G4. Which anti-virus program works better with Mac?

  • How do I get the original .ipa?

    I think I did something stupid, as I am known to do from time to time. I removed an .ipa file from my iTunes Media folder awhile ago, thinking it would delete the app. I guess I forgot and didn't pay attention and have been my merry way. Today I'm bo

  • Variables Not showing

    Hi All, I have added one variable to the query which is having already 4 variables, but when I tried to run it is showing two variables not even what I added.All variable including new one added is showing in the RSRT, but it is not showing in the Qu

  • WMP54GX To A WRVS4400N - SRX to N =108mbps?

    I currently have a WMP54GX Wireless-G w/ SRX PCI card that connects to a WRT54GX Wireless G w/ SRX. I get 108mbps because of the SRX. If I upgrade my WRT54GX to a WRVS4400N Wireless-N router, will it be backwards compatible with my SRX adapter, so I