You are not logged in.
By using ACPI I'm trying to get my laptop to hibernate at critical power state. So far I've made a few test scripts to test how to do this, with one of these scripts I'm stuck. My scripting skills are not that good to solve it.
This is the script:
if [ `awk '{print $3}' /proc/acpi/battery/C1E9/state` == "ok" ]
then
logger "battery is ok"
else
logger "battery is not ok"
fi
If I run the script I get an error (while "battery is not ok" is logged):
┌─[~]
└─> ./test2
./test2: line 1: [: too many arguments
┌─[~]
└─>
When I run the awk-command from the script, I get the following output:
┌─[~]
└─> awk '{print $3}' /proc/acpi/battery/C1E9/state
ok
discharging
2341
1824
10969
┌─[~]
└─>
I think the problem lies in the multiple output of the awk-command, but I'm stuck in solving it.
Any help would be great.
Last edited by NeoXP (2009-11-16 22:40:45)
Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."
Offline
Just change your if line to this one and teh error shouldn't occure anymore:
if [ "`awk '{print $3}' /proc/acpi/battery/C1E9/state`" == "ok" ]
The "" tell bash that all in it should be handled together as one string and not as per line one string.
Website: andrwe.org
Repository: repo.andrwe.org/<archtiecture>
Offline
Just change your if line to this one and teh error shouldn't occure anymore:
if [ "`awk '{print $3}' /proc/acpi/battery/C1E9/state`" == "ok" ]
The "" tell bash that all in it should be handled together as one string and not as per line one string.
Ok, the error message is gone now.
But the script is not doing as expected, because "battery is not ok" is logged (while state is ok).
Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."
Offline
Change you awk to
awk '{if (NR==2) {print $3}}'
Last edited by kazuo (2009-11-15 21:57:10)
Offline
That's because it's returning more than just "ok" as it prints field three of every line. You can get it to just print for the line you're interested in by putting a pattern in:
awk '/charging state/ {print $3}'
Note, check the wording I've used, I did it from memory.
See man awk (or gawk if you want to use GNU awk) for more details on patterns.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
Found a solution and put two test-scripts together:
if [ `awk '{print $2}' /proc/acpi/ac_adapter/C1E8/state` == "off-line" ];
then
logger "battery discharging"
if [ "grep -q ok /proc/acpi/battery/C1E9/state" ];
then
logger "battery ok"
else
logger "battery not ok"
/usr/sbin/pm-hibernate;
fi
else logger "battery charging"
fi
This is the idea:
The script first checks if the ac adapter is online, if not it checks the state of the battery. As long as it is "ok" nothing happens, if its not "ok" the system hibernates.
Then the last question, will it work when I put it in my handler.sh?
Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."
Offline
Have to keep working on it because in /proc/acpi/battery/C1E9/state it is still ok when battery-level is critical.
Thxs for now you all.
Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."
Offline
Change you awk to
awk '{if (NR==2) {print $3}}'
This tip from kazuo pointed me in the right direction, this is the script for now:
if [ `awk '{print $2}' /proc/acpi/ac_adapter/C1E8/state` == "off-line" ];
then
logger "battery discharging"
if (( `awk '{if (NR==5) {print $3}}' /proc/acpi/battery/C1E9/state` >= "250" ))
then
logger "battery ok"
else
logger "battery not ok"
/usr/sbin/pm-hibernate;
fi
else logger "battery charging"
fi
When run in debug mode this is the output:
┌─[~]
└─> ./test
++ awk '{print $2}' /proc/acpi/ac_adapter/C1E8/state
+ '[' off-line == off-line ']'
+ logger 'battery discharging'
++ awk '{if (NR==5) {print $3}}' /proc/acpi/battery/C1E9/state
+ (( 2914 >= 250 ))
+ logger 'battery ok'
┌─[~]
└─>
I have put the lines in my handler.sh, now I have to wait for the battery to discharge and see if it works.
Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."
Offline
Just FYI, and I wouldn't expect you to change a working script, but you could do the whole thing in awk:
awk '/off-line/ {print "battery discharging"}
/remaining/ {if ($3 > 250) print "battery ok"; else {print "battery not ok"; system("/usr/sbin/pm-hibernate")}}
/charging/' {print "battery charging"}' /proc/acpi/battery/<BAT>/state | Logger -
The formatting could probably be improved, but I'm a bit rusty and keep forgetting what's allowed.
Note: Untested + Possibility of typos.
Test it using
system("touch testxxx")
instead of your hibernate script.
Finally, *apparently* /proc is deprecated and we should be starting to use /sys if possible (it may not be on all systems).
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
@ skanky
Thanks for the suggestion, but you're right I'm not gonna change a working script. It is working exactly as expected.
@ all
When interested this is my current handler.sh
#!/bin/sh
# Default acpi script that takes an entry for all actions
# NOTE: This is a 2.6-centric script. If you use 2.4.x, you'll have to
# modify it to not use /sys
minspeed=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq`
maxspeed=`cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq`
setspeed="/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed"
set $*
case "$1" in
button/power)
echo "PowerButton pressed!">/dev/tty5
case "$2" in
PWRF) logger "PowerButton pressed: $2" ;;
*) logger "ACPI action undefined: $2" ;;
esac
;;
button/sleep)
case "$2" in
SLPB) echo -n mem >/sys/power/state ;;
*) logger "ACPI action undefined: $2" ;;
esac
;;
ac_adapter)
case "$2" in
AC)
case "$4" in
00000000)
echo -n $minspeed >$setspeed
#/etc/laptop-mode/laptop-mode start
;;
00000001)
echo -n $maxspeed >$setspeed
#/etc/laptop-mode/laptop-mode stop
;;
esac
;;
*) logger "ACPI action undefined: $2" ;;
esac
;;
battery)
if [ `awk '{print $2}' /proc/acpi/ac_adapter/C1E8/state` == "off-line" ];
then
logger "battery discharging"
if (( `awk '{if (NR==5) {print $3}}' /proc/acpi/battery/C1E9/state` >= "250" ))
then
logger "battery ok"
else
logger "battery not ok"
/usr/sbin/pm-hibernate;
fi
else logger "battery charging"
fi
;;
button/lid)
if [ `awk '{print $2}' /proc/acpi/button/lid/C1ED/state` == "closed" ];
then
logger "ACPI lid closed"
/usr/sbin/pm-suspend
fi
;;
*)
logger "ACPI group/action undefined: $1 / $2"
;;
esac
Still working on it though
Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."
Offline