You are not logged in.

#1 2012-01-25 10:37:55

capnion
Member
Registered: 2011-05-01
Posts: 10

Autohibernate with pm-utils and laptop-mode

Hi all,
I know this is a frequent problem but I cannot find any solution around the web.
I am using pm-utils and am able to suspend to disk and ram properly.
Now I want to hibernate automatically based on battery condition of my laptop.
I have installed acpi and laptop-mode-tool,  and both seem to work correctly, my battery status appear on conky, and every other events based on laptop-mode run smoothly.
I even wrote an /etc/laptop-mode/auto-hibernate.conf according to this post https://bbs.archlinux.org/viewtopic.php?id=50702.
But the problem is still there, the laptop doesn't autohibernate on low battery.
If anyone has managed to autohibernate based on low battery, I will glad to know his/her solution,
regards

Last edited by capnion (2012-01-25 10:38:18)

Offline

#2 2012-01-25 11:03:08

lucke
Member
From: Poland
Registered: 2004-11-30
Posts: 4,018

Re: Autohibernate with pm-utils and laptop-mode

Did you have a look at /etc/laptop-mode/conf.d/battery-level-polling.conf?

Offline

#3 2012-01-25 12:09:13

eldragon
Member
From: Buenos Aires
Registered: 2008-11-18
Posts: 1,029

Re: Autohibernate with pm-utils and laptop-mode

even if im not using the tools you pasted. this is what ive done (since my battery hardware is a bit shaky, i had to add some cornercases, which lead to me creating a new script).

some bits have been taken from the web (notify-send function for example).

see if it works for you. of course, input will be appreciated too!.

#!/bin/bash
# power management script for my crappy laptop
#BAT_FILE='/proc/acpi/battery/BAT0/state'
BAT_BASE='/sys/class/power_supply/BAT0/'
BAT_FILE='/sys/class/power_supply/BAT0/voltage_now'
PDIR="/run/"
LCK_FILE=${PDIR}`basename $0`.pid

SAFE_STATE='5000000'
MIN_STATE='10000000'
MAX_STATE='12500000'
THRESHOLD='10500000'
HIB_SECS=60


notify() {
    # Usage: notify "title" "description" [options to pass to send-notify"
    title="$1"
    body="$2"
    shift 2
    opts="$*"
    
    # Send messages to users listed by the 'users' command, but only once
    # Maybe sh has a better way of doing set operations, but this works:
    SENT=""
    for USER in $(users); do
        case $SENT in
            "$USER") ;;
            *" $USER") ;;
            "$USER "*) ;;
            *" $USER "*) ;;
            # use 'su USER' to send the message
            *) SENT="$SENT $USER";DISPLAY=:0 su $USER -c "notify-send $opts \"$title\" \"$body\"" ;;
        esac
    done
}


# test if we are root.
if [ $EUID != 0 ]; then
    sudo "$0" "$@"
    exit $?
fi



#test if we are running already

echo "LCK_FILE=$LCK_FILE"

if [ -f "${LCK_FILE}" ]; then
	MYPID=`head -n 1 "${LCK_FILE}"`
	TEST_RUNNING=`ps -p ${MYPID}|grep ${MYPID}`
	
	if [ -z "${TEST_RUNNING}" ]; then
		#the process is not running
		echo $$ > "$LCK_FILE"
	else
		echo "`basename $0` is already running [${MYPID}]"
		exit 0
	fi
else
	echo $$ > "$LCK_FILE"
fi

	

if [ -a "$BAT_BASE" ]; then 
	PRESENT=$(cat ${BAT_BASE}/present)
	if [ $PRESENT == '0' ]; then
		echo "Battery not present!"
		rm "${LCK_FILE}"
		exit 0
	else
		CURR_STATE=$(cat ${BAT_BASE}/voltage_now)
		CHAR_STATE=$(cat ${BAT_BASE}/status)
	fi
else
	echo "Something wrong, file missing!"
	rm "${LCK_FILE}"
	exit 1
fi

if [ $CURR_STATE -lt $SAFE_STATE ]; then
	echo "buggy batt, ignored result: $CURR_STATE"
	rm "${LCK_FILE}"
	exit 0
fi

if [ $CURR_STATE -lt $THRESHOLD ]; then
	if [ $CHAR_STATE != "Charging" ]; then
		echo "battery level low, hibernating in $HIB_SECS seconds"
		notify "BATTERY STATUS" "battery level low, hibernating in $HIB_SECS seconds. Connect to AC to cancel"
		sleep $HIB_SECS
		CHAR_STATE=$(cat ${BAT_BASE}/status)
		if [ $CHAR_STATE != "Charging" ]; then
			/usr/sbin/pm-hibernate
#			notify "BATTERY STATUS" "Comming out of hibernate!!"
				
			rm "${LCK_FILE}"
			exit 0
		fi
		notify "BATTERY STATUS" "Charger plugged. hibernate disabled"
	fi
else
	echo "battery state: $CHAR_STATE"
	echo "battery level: $CURR_STATE"
#	notify "battery level:" "$CURR_STATE"
fi
rm "${LCK_FILE}"

it is run by root every minute. it warns through notify-send one minute prior to hibernating, and aborts if the laptop was plugged within that minute.

some variables should be tweaked for your battery

EDIT: fixed a bug when testing if battery was being charged/discharged. added test for root.

Last edited by eldragon (2012-01-25 14:33:40)

Offline

#4 2012-01-25 13:14:36

capnion
Member
Registered: 2011-05-01
Posts: 10

Re: Autohibernate with pm-utils and laptop-mode

Thanks everyboy for your answers,

@Lucke: good tip, but unfortunatly it doesn't work for my laptop

@eldragon: Muchas gracias, maybe is my laptop as crappy as yours, so I will try your script as soon as possible. I'm using now this script https://bbs.archlinux.org/viewtopic.php?id=56646&p=67 , that is pretty good but has this sound/speacking warning inconvenience. Your solution looks more appealing for my needs.

Offline

Board footer

Powered by FluxBB