Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would love if someone could give me a script or something like that to make it do what I describe in the title...

share|improve this question
1  
... any reason why? seems a little dramatic if you are using your laptop and it shutdown because the battery is now full instead of empty! –  fossfreedom May 10 '12 at 22:22
    
Is your goal to fully charge your device while minimizing electrical usage without having to check battery status regularly? –  jrennie May 10 '12 at 22:30
    
Also, please note that your laptop will charge even when turned off (assuming it's plugged in and its battery hasn't been removed). In fact, it will probably charge faster when turned off than when turned on, since none of the power it's getting has to go to actually running the machine. –  Eliah Kagan Aug 21 '12 at 13:28
add comment

1 Answer 1

Here is a small python program which uses terminal to look into file for battery status and handles the operation with defined function. This will work for general case, but though, there might be a little problem. It uses the package "acpi" which monitors the battery status. The program is:

import commands
import pynotify
from threading import Timer


def battery_check():

    rem = float(commands.getoutput("grep \"^remaining capacity\" /proc/acpi/battery/BAT0/state | awk '{ print $3 }'"))
    full = float(commands.getoutput("grep \"^last full capacity\" /proc/acpi/battery/BAT0/info | awk '{ print $4 }'"))
    state = commands.getoutput("grep \"^charging state\" /proc/acpi/battery/BAT0/state | awk '{ print $3 }'")

    percentage = int((rem/full) * 100)

    if percentage == 100:
        pynotify.init("Battery Full! Now shutting down")
        os.system("sudo shutdown now -h -k") //shutdown command issued

    timer = Timer(300.0,battery_check)
    timer.start()

if __name__ == "__main__": battery_check()
share|improve this answer
    
This script looks reasonable to me. Note that the script will need to be run as root, e.g. sudo python ./battery.py (assuming the script is called battery.py and is located in the current dir). –  jrennie May 10 '12 at 23:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.