You are not logged in.

#1 2012-03-07 11:10:05

Lockheed
Member
Registered: 2010-03-16
Posts: 1,429

Run script only on battery

I have this script in cron that disables WiFi radio if there is no connection.
However, I would like to modify it so it disables the radio only if it is on Battery power. How should I do it?

#!/bin/bash

essid="`iwconfig wlan0 | grep ESSID | awk {'print $4'}`"
	if [ "$essid" == "ESSID:off/any" ] ; then
		sudo iwconfig wlan0 txpower off
	fi

Laptop: ThinkPad W500, C2D P9500, 8GB, Radeon RV635 (HD3650), Arch | Server/fw: Zotac AQ01, A4-5000 Kabini, 4GB, Arch/pfSense VM

Offline

#2 2012-03-07 11:41:02

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,838

Re: Run script only on battery

You can use acpi

pacman -S acpi
man acpi

Though the output varies from system to system.

Alternatively the information should be in a file under /sys/class/power_supply/ somewhere (again, it seems to vary).
Using acpi is simplest though and should be easier to maintain.


"...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

#3 2012-03-07 11:46:10

Lockheed
Member
Registered: 2010-03-16
Posts: 1,429

Re: Run script only on battery

I already have acpi installed and configured. I am talking about modifying to script as described, not about automatic power saving provided by 'acpi', 'laptop mode' etc.


Laptop: ThinkPad W500, C2D P9500, 8GB, Radeon RV635 (HD3650), Arch | Server/fw: Zotac AQ01, A4-5000 Kabini, 4GB, Arch/pfSense VM

Offline

#4 2012-03-07 12:02:51

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,838

Re: Run script only on battery

Lockheed wrote:

I already have acpi installed and configured. I am talking about modifying to script as described, not about automatic power saving provided by 'acpi', 'laptop mode' etc.

Sorry, I thought you just wanted to check battery state in the script. My misunderstanding.

Events are registered under /etc/acpi/events and a specified action is marked there.
I would guess you'll see a call to the /etc/acpi/actions/lm_battery.sh script in there if you're using laptop mode tools.

You could switch that with a call to your own script, and add the lm stuff to your script, so it doesn't get lost.

Alternatively you could change /etc/acpi/handler.sh as detailed here: https://wiki.archlinux.org/index.php/Acpid

Not sure which is the best way.


"...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

#5 2012-03-07 12:03:45

vacant
Member
From: downstairs
Registered: 2004-11-05
Posts: 799

Re: Run script only on battery

Try the following running on a/c then just battery

acpi -b

Can't you modify your script to include this command and act on the output?

Offline

#6 2012-03-07 12:07:48

oded
Member
From: Israel
Registered: 2009-08-06
Posts: 26

Re: Run script only on battery

You can also check for the AC status like so:

 ~ %: cat /sys/class/power_supply/AC/online                                                                                                                                                                                                                                   
0

Whatever works for you (:

Offline

#7 2012-03-07 12:13:29

Lockheed
Member
Registered: 2010-03-16
Posts: 1,429

Re: Run script only on battery

OK. I did not express myself clearly. I know how to check whether the AC is engaged. I just don't know how to introduce this information to the script.


Laptop: ThinkPad W500, C2D P9500, 8GB, Radeon RV635 (HD3650), Arch | Server/fw: Zotac AQ01, A4-5000 Kabini, 4GB, Arch/pfSense VM

Offline

#8 2012-03-07 12:17:15

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,838

Re: Run script only on battery

oded wrote:

You can also check for the AC status like so:

 ~ %: cat /sys/class/power_supply/AC/online                                                                                                                                                                                                                                   
0

Whatever works for you (:

$cat /sys/class/power_supply/AC/online 
cat: /sys/class/power_supply/AC/online: No such file or directory

I know you said whichever works, but I thought it worth pointing out that the structure under /sys/class/power_supply/ can vary quite a bit - it's changed on my machine so that old versions of scripts wouldn't work now. That's why I switched to acpi. This is not just for future changes on this machine, but should mean that the script still works on a new one.


"...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

#9 2012-03-07 12:22:26

Cloudef
Member
Registered: 2010-10-12
Posts: 636

Re: Run script only on battery

mything() {
# do your thing here
}

while 1
do
[[ "$(cat /sys/class/power_supply/AC/online)" == "0" ]] && mything
sleep 30 # Set a good interval here to save wakeups
done

Turn the script either a daemon (/rc.d script) then you can start/stop it. Or start it manually from rc.local, .xinitrc or some other place.
Alternatively you can use acpid handlers like mentioned above, it should have AC power on/off actions as well, if I remember right.

(acpid solution should be cleaner as well, since you have one daemon monitoring state changes within your system and starting scripts only when needed. Instead of having dozen of scripts in infinite loop waiting for something to happen)

Last edited by Cloudef (2012-03-07 12:29:22)

Offline

#10 2012-03-07 12:25:31

vacant
Member
From: downstairs
Registered: 2004-11-05
Posts: 799

Re: Run script only on battery

Lockheed wrote:

OK. I did not express myself clearly. I know how to check whether the AC is engaged. I just don't know how to introduce this information to the script.

You could adapt this, cron runs it and notifies me when the battery is very low

#!/bin/bash

BATTINFO=`acpi -b`
if [[ `echo $BATTINFO | grep Discharging` && `echo $BATTINFO | cut -f 5 -d " "` < 00:15:00 ]] ; then
    DISPLAY=:0.0 /usr/bin/notify-send "low battery" "$BATTINFO"
fi

I think checking the battery state rather than the a/c adaptor state will give you a better experience, e.g. no point in closing wifi if you happen to move power outlet

Offline

#11 2012-03-07 12:25:46

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,838

Re: Run script only on battery

Lockheed wrote:

OK. I did not express myself clearly. I know how to check whether the AC is engaged. I just don't know how to introduce this information to the script.

You can either set the script to run under acpi events as shown above, or have it as a first check in your script:

#!/bin/bash

essid="`iwconfig wlan0 | awk ' /ESSID/ {print $4}'`"
BAT=$(acpi -b | awk '{print $3}')
if [ "$BAT" == "Discharging" ]; then
	if [ "$essid" == "ESSID:off/any" ] ; then
		sudo iwconfig wlan0 txpower off
	fi
fi

Obviously your awk statement for the BAT line and the value of BAT will probably be different.
NB this script is untested and could probably be improved, but I'm doing work avoidance, so it's a bit rushed.

EDIT: too slow. smile

Last edited by skanky (2012-03-07 12:26:38)


"...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

#12 2012-03-07 12:26:51

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,493
Website

Re: Run script only on battery

Lockheed wrote:

OK. I did not express myself clearly. I know how to check whether the AC is engaged. I just don't know how to introduce this information to the script.

Then just take your favorite method of checking whether AC is engaged and add a line like

if [[ <AC is engaged test> == <expected result when AC is not engaged> ]]; then

to the top of your script, and a 'fi' at the bottom.

One way that I've used is

if [[ `acpi | grep "disconnected"` ]]; then

I'm not on the computer that has this, so I don't remember whether that is true when connected or disconnected, but either way will as long as you put the right parts after it.


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#13 2012-03-07 12:32:56

vacant
Member
From: downstairs
Registered: 2004-11-05
Posts: 799

Re: Run script only on battery

We're a helpful bunch smile

Offline

#14 2012-03-07 12:41:12

Lockheed
Member
Registered: 2010-03-16
Posts: 1,429

Re: Run script only on battery

I have done it this way (run by root cron):

#!/bin/bash

if [[ cat /sys/class/power_supply/AC/online == 0 ]]; then
essid="`iwconfig wlan0 | grep ESSID | awk {'print $4'}`"
	if [ "$essid" == "ESSID:off/any" ] ; then
		sudo iwconfig wlan0 txpower off
	fi

but it does not seem to be working.


Laptop: ThinkPad W500, C2D P9500, 8GB, Radeon RV635 (HD3650), Arch | Server/fw: Zotac AQ01, A4-5000 Kabini, 4GB, Arch/pfSense VM

Offline

#15 2012-03-07 13:23:26

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,838

Re: Run script only on battery

You're missing a fi at the end - though that could be c&p error?

Last edited by skanky (2012-03-07 13:26:01)


"...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

#16 2012-03-07 14:15:06

Lockheed
Member
Registered: 2010-03-16
Posts: 1,429

Re: Run script only on battery

.scripts/disableunusedWLAN: line 3: conditional binary operator expected
.scripts/disableunusedWLAN: line 3: syntax error near `/sys/class/power_supply/AC/online'
.scripts/disableunusedWLAN: line 3: `if [[ cat /sys/class/power_supply/AC/online == 0 ]]; then'

Laptop: ThinkPad W500, C2D P9500, 8GB, Radeon RV635 (HD3650), Arch | Server/fw: Zotac AQ01, A4-5000 Kabini, 4GB, Arch/pfSense VM

Offline

#17 2012-03-07 14:18:46

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,493
Website

Re: Run script only on battery

Not only missing the closing 'fi', you'll probably need to enclose the cat <filename> in either a $(...) or `...`

#!/bin/bash

if [[ "$(cat /sys/class/power_supply/AC/online)" == "0" ]]; then
essid="`iwconfig wlan0 | grep ESSID | awk {'print $4'}`"
	if [ "$essid" == "ESSID:off/any" ] ; then
		sudo iwconfig wlan0 txpower off
	fi
fi

I've also added some quotes for good measure which may help in some circumstance, but have really just become a compulsive habbit.  And a good habit, I'd argue, as it never hurts to have them, but sometimes hurts not to.

As an example, here's an excerpt from one of my scripts that I use to put a battery monitor in my taskbar.  There are some conditional elements that display the info differently if my computer is plugged in.  For example:

if [[ `acpi -a` =~ "off-line" ]]; then
    # do stuff only when running on battery
fi

Last edited by Trilby (2012-03-07 14:24:35)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#18 2012-03-07 14:23:16

Lockheed
Member
Registered: 2010-03-16
Posts: 1,429

Re: Run script only on battery

Working now. Thanks!


Laptop: ThinkPad W500, C2D P9500, 8GB, Radeon RV635 (HD3650), Arch | Server/fw: Zotac AQ01, A4-5000 Kabini, 4GB, Arch/pfSense VM

Offline

Board footer

Powered by FluxBB