On my machine the current state of the battery can be read-off from /proc/acpi/battery/BAT1/state
Sample content of this file:
present: yes
capacity state: ok
charging state: charging
present rate: 749 mA
remaining capacity: 530 mAh
present voltage: 11200 mV
You can locate the appropriate file on your machine and use this to suspend when battery is in critically low power. The following python script will do it for you:
#!/usr/bin/python
import os
import time
battery_state_file = "/proc/acpi/battery/BAT1/state"
critically_low_power = 500
suspend_command = "sudo pm-suspend"
while True:
time.sleep(1)
f = open(battery_state_file)
content = f.read().split('\n')
f.close()
if content[2].split()[2] == "discharging":
if int(content[4].split()[2]) < 500:
os.system(suspend_command)
Save this to a file (say) suspendscript
, make it executable, add suspendscript &
to .xsessionrc in your home folder, logout and log back in.
You have to set battery_state_file and critically_low_power appropriately. If remaining capacity drops below 'critically_low_power' mAh, the system will suspend. Optional: You can, if you want, use "present rate" and "remaining capacity" to find "expected time left" (= "remaining cap."/"present rate" hours) and suspend if time left drops below a certain threshold.
Notes:
- This code will have to be modified if the content of the statefile is not in the same format as the sample content given above.
sudo pm-suspend
requires the sudoer to give his password. You have to make pm-suspend
sudoable without password. See how: How do I run specific sudo commands without a password? .
Alternatively, you can change suspend_command to simply pm-suspend
and add sudo suspendscript &
to .xsessionrc and make suspendscript
sudoable without password.
- If you use this script, you have to set 'critically low power action' to 'do nothing' in gnome-power-manager settings.