# Hibernate when battery is critically low [Solved]

## replazer

I`m trying to setup my laptop to hibernate when the battery is critically low. Now its just shutting down when the battery dies, without any warnings or anything.

```
cat /proc/acpi/battery/BAT0/info
```

gives that the battery is discharging and giving remaining mAh,etc ..

I`m running on a Dell latitude D620 with gnome, pm-utils to hibernate/suspend and have installed gnome-power-manager. Btw, everything else in gnome-power-manager seems to work fine.Last edited by replazer on Mon Jan 05, 2009 10:16 am; edited 1 time in total

----------

## Wormo

I don't know how to do it with gnome-power-manager, but gkrellm battery monitor works well for this purpose. I've got one alert set up to pop up warnings every minute or so when the battery is quite low, and another that runs hibernate when critically low.

----------

## replazer

how have you set up the gkrellm to hibernate ???

----------

## DirtyHairy

You could also use laptop-mode-tools, works for me.

----------

## replazer

how do you set up laptop-mode-tool to hibernate on low battery?

----------

## replazer

DirtyHairy, could you post your laptop-mode.conf please ??? Do you have to restart laptop-mode every time you hibernate to make it work again??

----------

## Wormo

 *replazer wrote:*   

> how have you set up the gkrellm to hibernate ???

 

I have an alert call 'hibernate' command from sys-power/hibernate-script

----------

## replazer

Where do you put this command in gkrellm? I`m using pm-utils btw....

----------

## Wormo

 *replazer wrote:*   

> Where do you put this command in gkrellm? I`m using pm-utils btw....

 

Open up gkrellm configuration

select 'battery' monitor

make sure Enable Battery is checked

go to setup tab, turn on 'Alerts check for percent capacity'

click 'Alerts' button, set your alarm limit

type your command to hibernate into 'Alarm command' 

mine is 'sudo /usr/sbin/hibernate' -- using sudo to allow my normal user to run the privileged hibernate command 

you would need to figure out what the equivalent is for pm-utils

----------

## replazer

thanks, I`ve set it up now but i still have one problem.

When i run gkrellm from the applications menu, nothing happens. When i run it from a terminal i see that gkrellm is asking for my password to run the hibernate command, do you know how to fix this ??

----------

## Wormo

You need to set up your sudo so that the user can execute this command without entering a password.

Example from my /etc/sudoers

```

%wheel  ALL=(ALL)   NOPASSWD: /usr/sbin/hibernate

```

This authorizes users who have been added to wheel group (such as myself...) to run hibernate without typing a password

----------

## .yankee

 *Wormo wrote:*   

> You need to set up your sudo so that the user can execute this command without entering a password.
> 
> 

 

Isn't it safer to use laptop-mode-tools? And, apart from hibernating, it gives a large number of other useful, mainly power saving features.

----------

## Wormo

 *.yankee wrote:*   

>  *Wormo wrote:*   You need to set up your sudo so that the user can execute this command without entering a password.
> 
>  
> 
> Isn't it safer to use laptop-mode-tools? And, apart from hibernating, it gives a large number of other useful, mainly power saving features.

 

I don't see what's safer about laptop-mode-tools... a rule allowing users to hibernate seems pretty innocuous to me.

Last time I looked into laptop mode, there was some controversy about power saving vs disk wear from too much parking. Feel free to tell us about the cool features in laptop-mode-tools that you use though  :Smile: 

----------

## .yankee

 *Wormo wrote:*   

> 
> 
> Last time I looked into laptop mode, there was some controversy about power saving vs disk wear from too much parking. 

 

Well, if you are troubled with that, you can safely tell the l-m-t to set the Power Management feature to be no less than 128, which does not permit spindown.

 *Wormo wrote:*   

> 
> 
> Feel free to tell us about the cool features in laptop-mode-tools that you use though 
> 
> 

 

Well, I can say what I see as benefits at least  :Smile: 

 You can easily make l-m-t execute any scripts you wish on a specific event (like: when starting/stopping to work on batteries/ac)

 You can setup some sysfs parameters to be changed at the said events, thus compromising between power consumption, system responsiveness (though that requires to do some reading first  :Wink: )

 You can set how cpu-governor, cpu-frequency and cpu-throttling changes in the said situations.

...Also the lid state can be taken into consideration by l-m-t (you can, for ex., make your system hibernate some time after the lid's been closed, when on battery).

----------

## DirtyHairy

Weeha, a long thread on a seemingly innoncent topic  :Wink:  I have been using laptop-mode-tools on this laptop for years, and it works nice and cleanly for setting CPU speeds and disk power managment without depending on running something under X, that's why I use it. I must admit that I don't regularely use the suspend-on-low-power feature as I usually manually hibernate before, but the approximately 2-3 times per year when it happens, it works.

I'd like to spare you the pain of posting the while laptop-mode.conf, but here is the relevant section:

```
###############################################################################

# Auto-hibernation settings

# -------------------------

#

# Using these settings, you can make laptop mode tools automatically put your

# computer into hibernation when the battery level goes critically low.

#

# This feature only works on ACPI, and only works on computers whose batteries

# give off battery events often enough.

###############################################################################

# Should laptop mode tools perform auto-hibernation?

ENABLE_AUTO_HIBERNATION=1

# The hibernation command that is to be executed when auto-hibernation

# is triggered.

HIBERNATE_COMMAND=/usr/sbin/hibernate-ram

# Auto-hibernation battery level threshold, in percentage of the battery's

# total capacity.

AUTO_HIBERNATION_BATTERY_CHARGE_PERCENT=4

# Enable this to auto-hibernate if the battery reports that its level is

# "critical".AUTO_HIBERNATION_ON_CRITICAL_BATTERY_LEVEL=1
```

Note that I use hibernate-script to suspend to RAM, this can be used for suspend-to-disk, too. Concerning disk wear: I don't have the numbers in my head right now, but taking the number of spin-up / down / park cycles modern laptop drives can tolerate before starting to fail, you typically find (depending on the work you do, of course) > 5 years of fail-free operation - enough for me. In addition, this can be nicely monitored using SMART. Of course, there's no gurantee that the drive will NOT fail, but drives can fail for many reasons... And anyway, if you don't feel comfortable, you can just disable disk power managment in laptop-mode-tools  :Smile: 

----------

## replazer

Is there anything in particular I have to do when I install/setup the laptop-mode-tools ???? I`ve edited the laptop-mode.conf to autohibernate when the battery have gone down to 10% but nothing happens automaticly after the acpi shows that the battery is under 10%, if I run #sudo laptop_mode it immediately starts to hibernate....

Any advice??

----------

## replazer

Thank you Wormo! I got it working with gkrellm now:)

----------

## DirtyHairy

I've set up the configuration and added it to the default runlevel, and that's it...

----------

## .yankee

 *replazer wrote:*   

> Is there anything in particular I have to do when I install/setup the laptop-mode-tools ???? I`ve edited the laptop-mode.conf to autohibernate when the battery have gone down to 10% but nothing happens automaticly after the acpi shows that the battery is under 10%, if I run #sudo laptop_mode it immediately starts to hibernate....
> 
> Any advice??

 

As DirtyHairy suggested, ensure that it is started - if you are not sure about it, check by issuing rc-status. It should be listed in the output as "[started]". If it didn't start at boot, add it to the default runlevel by  rc-config add laptop_mode default.

----------

## replazer

hmmm, it looks like I`ve got it working:

These are my settings in laptop-mode.conf:

ENABLE_AUTO_HIBERNATION=1

HIBERNATE_COMMAND=/usr/sbin/pm-hibernate

AUTO_HIBERNATION_BATTERY_CHARGE_PERCENT=10

```

stian@D620 ~ $ /etc/init.d/laptop_mode status

 * status: started

stian@D620 ~ $ sudo laptop_mode

Laptop mode enabled, active [unchanged]
```

Also the acpi seems to be reading my battery status correctly:

```

stian@D620 ~ $ acpi

     Battery 1: discharging, 88%, 02:12:13 remaining
```

----------

## DirtyHairy

Hmm, laptop-mode-tools depends on your BIOS generating ACPI events when the battery status changes, so if it doesn't work for you, the reason may be a somewhat broken ACPI BIOS.

----------

## michel7

im using these scripts for hibernation on low battery level, you need to install xosd for grafical output

batcheck.sh

```

#!/bin/bash

LIMIT="1800" ## Suspend if battery level drops below this (in mAh/mWh)

WLIMIT="4000" ## Visual warning to user if battery level drops below this (in mAh/mWh)

SLEEP="20"  ## Seconds between each battery level check

BAT="BAT0" ## Part of path: /proc/acpi/battery/BAT/

HIBERNATE="/etc/acpi/s2ram.sh" ## command used to suspend

WARNING="/etc/acpi/warning.sh" ## command used to warn user

while [ true ]; do

  if [ -e "/proc/acpi/battery/$BAT/state" ]; then

     PRESENT=$(/bin/sed -ne "/present:/{s/^present:[ ]*\([a-z]*\)$/\1/p;q}" /proc/acpi/battery/$BAT/state)

     #echo $PRESENT

     if [ "$PRESENT" = "yes" ]; then

        STATE=$(/bin/sed -ne "/charging state:/{s/^charging state:[ ]*\([a-zA-Z]*\)$/\1/p;q}" /proc/acpi/battery/$BAT/state)

        BATTERY=$(/bin/sed -ne "/remaining capacity:/{s/^remaining capacity:[ ]*\([0-9]*\) m[WA]h$/\1/p;q}" /proc/acpi/battery/$BAT/state)

        #echo $BATTERY

        #echo $STATE

        if [ "$BATTERY" -lt "$WLIMIT" ] && [ "$STATE" = "discharging" ]; then

           ## Comment out the following line if you don't

           ## want to log the event to system log:

           #logger "Battery at ${BATTERY} mWh. Warning to user."

           ## Warn:

           "$WARNING"

       fi

        if [ "$BATTERY" -lt "$LIMIT" ] && [ "$STATE" = "discharging" ]; then

           ## Comment out the following line if you don't

           ## want to log the event to system log:

           #logger "Battery at ${BATTERY} mWh. Suspending to disk."

           #echo "Battery at ${BATTERY} mWh. Suspending to disk."

           ## Suspend:

           "$HIBERNATE"

       fi

     fi

  fi

  sleep ${SLEEP}s

done

```

s2ram.sh

```

#!/bin/sh

XAUTHORITY=<PATH_TO_YOUR_XAUTHORITY>\.Xauthority; export XAUTHORITY; export DISPLAY=:0.0;

echo "Suspending ..." | osd_cat -f "-*-helvetica-*-r-*-*-24-*-*-*-*-*" -c Red -s 1 -p middle -A center -d 2;

echo mem > /sys/power/state

```

warning.sh

```

#!/bin/sh

XAUTHORITY=<PATH_TO_YOUR_XAUTHORITY>\.Xauthority; export XAUTHORITY; export DISPLAY=:0.0;

echo "Low battery level" | osd_cat -f "-*-helvetica-*-r-*-*-24-*-*-*-*-*" -c Red -s 1 -p middle -A center -d 2;

```

----------

## replazer

Now I got it working with both gkrellm and gnome-power-manager ... thats enough!  :Very Happy: 

Thanks for all your help!

----------

