On my machine the current state of the battery can be read-off from /proc/acpi/battery/BAT1/state
Sample content of this file:
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:
battery_state_file = "/proc/acpi/battery/BAT1/state"
critically_low_power = 500
suspend_command = "sudo pm-suspend"
f = open(battery_state_file)
content = f.read().split('\n')
if content.split() == "discharging":
if int(content.split()) < 500:
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.
- 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: Specific sudo commands without 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.