You are not logged in.

#1 2011-01-14 06:42:12

empthollow
Member
Registered: 2009-09-26
Posts: 168

Bash daemon cpu usage [solved]

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

#2 2011-01-14 19:26:04

Alm
Member
Registered: 2010-09-15
Posts: 64

Re: Bash daemon cpu usage [solved]

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

#3 2011-01-15 06:07:47

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: Bash daemon cpu usage [solved]

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

#4 2011-01-15 09:42:41

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: Bash daemon cpu usage [solved]

You can use ACPI to catch and handle these events without the ugly polling.

Offline

#5 2011-01-16 18:08:07

Watermel0n
Member
Registered: 2010-03-10
Posts: 60

Re: Bash daemon cpu usage [solved]

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

#6 2011-01-20 03:36:32

empthollow
Member
Registered: 2009-09-26
Posts: 168

Re: Bash daemon cpu usage [solved]

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

#7 2011-01-20 10:33:06

Damnshock
Member
From: Barcelona
Registered: 2006-09-13
Posts: 414

Re: Bash daemon cpu usage [solved]

empthollow wrote:

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 smile

Regards


My blog: blog.marcdeop.com
Jabber ID: damnshock@jabber.org

Offline

Board footer

Powered by FluxBB