I am using Ubuntu 14.04 LTS. My battery is not working, i.e. it provides backup for around 5 minutes. Now when I download stuff, it sometimes takes around 5 - 6 hours. And I can't just hang around the laptop for so long. So I wish to make a code, that will check if battery is charging every 5 minutes, and if not, it will shutdown the system.
-
Any other requirements other than shutdown when fully charged ?– Sergiy KolodyazhnyyMay 3, 2016 at 12:22
-
Also, do you use the default desktop, Unity ?– Sergiy KolodyazhnyyMay 3, 2016 at 12:57
-
Answer posted, let me know what you think or if you need more info– Sergiy KolodyazhnyyMay 3, 2016 at 14:40
Add a comment
|
2 Answers
Try this python script. It borrows from Saving work automatically when battery is low
#!/usr/bin/env python
import subprocess
import dbus
sys_bus = dbus.SystemBus()
ck_srv = sys_bus.get_object('org.freedesktop.ConsoleKit',
'/org/freedesktop/ConsoleKit/Manager')
ck_iface = dbus.Interface(ck_srv, 'org.freedesktop.ConsoleKit.Manager')
stop_method = ck_iface.get_dbus_method("Stop")
battery_limit = 90 # in percent
def get_battery_percentage():
percentage, err = subprocess.Popen([r'upower -i $(upower -e | grep BAT) | grep --color=never -E percentage | xargs | cut -d ' ' -f2 | sed s/%//
'], shell=True, stdout=subprocess.PIPE).communicate()
return(int(percentage))
while True:
if get_battery_percentage() <= battery_limit:
stop_method()
The script bellow uses two calls to dbus
and one while loop to poll for the percentage. Very simple and effective setup. Run this when you want to shutdown the laptop once it is charged
#!/bin/bash
get_percentage()
{
qdbus org.gnome.SettingsDaemon.Power \
/org/gnome/SettingsDaemon/Power \
org.gnome.SettingsDaemon.Power.Percentage
}
shutdown_system()
{
qdbus com.canonical.Unity \
/com/canonical/Unity/Session \
com.canonical.Unity.Session.Shutdown
}
# Basically loop that waits till
# battery reaches 100%. When 100%
# reached , loop exits, and next command
# is executed, which is shutdown
while [ $(get_percentage) -ne 100 ] ;
do
sleep 0.25
done
# Add delay or a warning message here if necessary
shutdown_system
-
@ShohanDebnath: Answer accepted by you, but is not adding a condition on 'download-end' necessary in yr use-case ?– CbhiheMay 4, 2016 at 6:56