Sunday, November 25, 2007

Are We Running on AC Power?

One of the nice things about using a laptop as a desktop substitute is its battery. This proved important lately as we experienced several successive power failures. We finally managed to find the culprit - a faulty electrical appliance.

In the meantime I needed a way to disable backups when my laptop was running on batteries, because the the external USB backup disk runs on mains power. This is easy enough using the script on_ac_power (part of the powermgmt-base package) - define the following function

check_ac_power ()
{
# must be on AC power
echo -n "Checking AC Power ... "
( on_ac_power > /dev/null && ( echo "ON" ; return 0 ) ) || ( echo "OFF" ; return 1 )
}

and run it at the beginning of the script that's executed by Bacula with the RunBeforeJob directive:

check_ac_power || exit $?

The exit $? bit is used to return the status that was returned by check_ac_power, if it fails (i.e. non-zero status code), to Bacula.

[14 Jul. 2008] UPDATE: the on_ac_power script stopped working after a kernel upgrade... (I've also revised and fixed the contents of this entry).

No comments:

Post a Comment