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?
Tell me more
×
Ask Ubuntu is a question and answer site for
Ubuntu users and developers. It's 100% free, no registration required.
|
I would expand a bit on the first answer, and instead do something like this for a cron entry:
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. |
|||
|
|
|
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 |
|||
|
|
|
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 |
|||
|
|