You are not logged in.
Pages: 1
I wrote a bash script that checks if powere is plugged in via 'acpi -a' every minute. Although it sleeps for 60 seconds between checks it still runs my cpu from 6% to around 72%. Interestingly, this cpu jump seems to happen only when initially started, and only when my laptop is plugged in. This script is to check battery percentage and if not plugged in and notify when the battery is low.
I would like to know if anyone has any input as to why this uses so much cpu and how to correct this.
here is the script
#!/bin/bash
. /etc/conf.d/bwd.conf
while [ "$continue" = "1" ]; do
while [ -z "$(acpi -a | grep on)" ]; do
if [ -n "$delay" ]; then
sleep $delay
fi
until [ "$(acpi | grep -o [0-9]*[0-9]% | grep -o [0-9]*[0-9])" -le "$warning1" ]; do
if [ -n "$delay" ]; then
sleep $delay
fi
done
notify-send "WARNING - LOW BATTERY" "Battery has $(acpi | grep -o [0-9][0-9]:[0-9][0-9]:[0-9][0-9]) left."
until [ "$(acpi | grep -o [0-9]*[0-9]% | grep -o [0-9]*[0-9])" -le "$warning2" ]; do
if [ -n "$delay" ]; then
sleep $delay
fi
done
echo acpi
if [ -z "$(acpi -a | grep on)" ]; then
notify-send "WARNING - LOW BATTERY" "$mesg2"
fi
sleep $seconds
if [ -z "$(acpi -a | grep on)" ]; then
$suspend_command
fi
done
done
here are the acpi events that occur when I unplug the laptop.
ac_adapter AC0 00000080 00000000
battery BAT0 00000080 00000001
hotkey ATKD 00000051 0000000d
processor CPU0 00000081 00000000
processor CPU1 00000081 00000000
Thanks for you input.
Last edited by empthollow (2011-01-20 03:36:53)
--empthollow
Check out my Arch based live distro http://fluxcapacity.99k.org
Offline
You should check the contents of $delay and $seconds. As is, it seems both variables are not initialized, so your script will not sleep at any point (which would then explain the high cpu usage).
See, you're unpacking.
Offline
I'm not exactly sure what you're trying to accomplish; your script seems to me to be prone to high cpu-useage loops. If all you want is a warning message if your battery is low when your adaptor is not plugged in, then you could try the following script.
One caveat, I don't have a laptop and I don't know what the output of
acpi -b
looks like, so I don't know which field contains the battery charge info. In the script below, I'm guessing that it is field number 3, so if it is different than that, the script below is incorrect and will not work.
If you don't understand what I am talking about, run
acpi -b
on your laptop, and post the output for me.
#!/bin/bash
trap "exit" SIGINT SIGTERM
delay=60
warning=50
while :
do
adapter=$(acpi -a | grep on)
b=$(acpi -b | cut -d ' ' -f 3)
[[ -z "$adapter" && "$b" -lt "$warning" ]] &&
notify-send "WARNING - LOW BATTERY" "Battery has ${b}% left."
sleep $delay
done
Last edited by rockin turtle (2011-01-15 06:08:45)
Offline
You can use ACPI to catch and handle these events without the ugly polling.
Offline
Yes, polling is bad. See, if you poll for changes every 60 seconds, 99% of those polls are still unnecessary, because nothing changed. Instead, just let acpid trigger your script in the event the battery is low.
Last edited by Watermel0n (2011-01-16 18:08:25)
Offline
Thanks for all your input. I agree polling is bad. I found a way to reduce the cpu usage but still use polling. I am not very familiar with acpid configuration, so I didn't think of doing it that way. I'll check the archwiki.
--empthollow
Check out my Arch based live distro http://fluxcapacity.99k.org
Offline
Thanks for all your input. I agree polling is bad. I found a way to reduce the cpu usage but still use polling. I am not very familiar with acpid configuration, so I didn't think of doing it that way. I'll check the archwiki.
Make sure to share your results
Regards
My blog: blog.marcdeop.com
Jabber ID: damnshock@jabber.org
Offline
Pages: 1