Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm setting up a cron job to update my nmzmail database periodically, but I'd like the cron job to execute only if my laptop is plugged in. Is there a way to do this? If there's not a built in cron command, then is there a way to check via the command line that I could use in a simple script?

share|improve this question
up vote 1 down vote accepted

I would expand a bit on the first answer, and instead do something like this for a cron entry:

[ $(cat /proc/acpi/ac_adapter/AC/state | awk '{print $NF}') = 'on-line' ] && /path/to/your/script_file

I would rather not even start my script, unless the test is successful. In this instance cron will never start the script, if the laptop is on battery.

share|improve this answer
Perfect, thank you! – Brad Jun 23 '11 at 19:32

A simpler version using awk :

[ $(awk '{print $NF}' /proc/acpi/ac_adapter/AC/state) = 'on-line' ] && /path/to/your/script_file

but this version still looks simpler IMHO :

grep -q on-line /proc/acpi/ac_adapter/AC/state && /path/to/your/script_file
share|improve this answer

What you could do is to check if /proc/acpi/ac_adapter/AC/state contains the word on-line. You could put the following in at the beginning of your cron job :

# Do nothing when no AC power is available
grep -q on-line /proc/acpi/ac_adapter/AC/state || exit 0
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.