Click to See Complete Forum and Search --> : Battery vs. AC for a shell script?


JThundley
03-23-2004, 07:37 AM
I'd like to write a shell script to do stuff only if I'm on AC and not running just on battery power. I checked dcop, I thought there might be something in klaptop, but there was nothing. I thought there might be something I could use in /proc but I got lost really quickly. Searches were all too general.

Has anyone ever written a shell script that has to do with this?

If anyone's wondering, I want to make a cronjob to start setiathome every night, but only if I'm plugged into the wall. I don't need to kill my battery looking for aliens ;)

terribleRobbo
03-23-2004, 08:00 AM
Use perl or grep to parse the output of 'apm'.

Eg. it usually outputs something like:

AC on-line, battery status high: 99% (3:27)


So, a simple:

if [ `/usr/bin/apm | grep -c "AC\ on-line"` -eq "1" ]; then
search_for_little_green_men &
fi

... should do the trick.

Icarus
03-23-2004, 08:12 AM
With the 2.4 and 2.6 kernels it is /proc/acpi/ac_adapter/AC/state and the two states it is in is
state: on-line
state: off-line
in the cron job you can cat and grep the status and use that to determine to run SETI or not

cat /proc/acpi/ac_adapter/AC/state | cut -c26-32

For me that will show the state of on-line or off-line (test with yours to verify)

I'll let you figure out the if/then statement to use :)

JThundley
03-23-2004, 05:27 PM
Absolutely perfect! Thanks guys!