Is there a way to suspend when Ubuntu is at critically low power. The only option in Ubuntu 11.10 power setting is to shutdown or hibernate. I run my battery all the way down till it suspends then i plug it in. It gets annoying when my computer hibernates at critically low power.

Thanks in advance for the help


Not sure why power settings only offers hibernate or shutdown.
You can change the option to suspend using dconf-editor:
Browse to org->gnome->settings-daemon->plugins->power, and change the value of critical-battery-action to "suspend".


Just a hunch, but I believe that at critically low power, the system doesn't have enough power to even sustain suspend mode - yes, the suspend mode, though is low on power consumption, draws power to keep data on the RAM. So, even if the system suspends, it will immediately power off. This is probably why jpd's solution didn't work.

Why I believe so? I've noticed that after the system powers off due to critically low power it is impossible (at least on my laptop) to turn it on without plugging it in. So critically low power must mean 'cannot carry on' and not 'just 5 minutes left' or something like that.

I think what you want is for the system to suspend when about 10 minutes of battery is left. I don't know how to attack that problem, but I believe Unity notifies you about this. In my case this notification is sufficient.


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:

  1. 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.
  2. 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.
  3. If you use this script, you have to set 'critically low power action' to 'do nothing' in gnome-power-manager settings.