You are not logged in.
Pages: 1
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
fiOffline
You can use acpi
pacman -S acpi
man acpiThough 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
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.
Offline
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
Try the following running on a/c then just battery
acpi -bCan't you modify your script to include this command and act on the output?
Offline
You can also check for the AC status like so:
~ %: cat /sys/class/power_supply/AC/online
0Whatever works for you (:
Offline
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.
Offline
You can also check for the AC status like so:
~ %: cat /sys/class/power_supply/AC/online 0Whatever works for you (:
$cat /sys/class/power_supply/AC/online
cat: /sys/class/power_supply/AC/online: No such file or directoryI 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
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
doneTurn 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
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"
fiI 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
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
fiObviously 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. ![]()
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
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> ]]; thento the top of your script, and a 'fi' at the bottom.
One way that I've used is
if [[ `acpi | grep "disconnected"` ]]; thenI'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.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
We're a helpful bunch ![]()
Offline
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
fibut it does not seem to be working.
Offline
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
.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'Offline
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
fiI'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
fiLast edited by Trilby (2012-03-07 14:24:35)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Working now. Thanks!
Offline
Pages: 1