Android and Linux

Sunday, January 16, 2011

61 minute cron

It seems like everyone who uses cron to schedule jobs in Linux eventually runs into a problem where they need to schedule a command at odd intervals, like 61 minutes. Cron can easily run every hour, but 61 minutes is harder to achieve.

The normal methods include using a sleep command or various rather elaborate methods in the script itself to fire off every 61 minutes.

A much simpler method is using cron's cousin, the at command. The at command will run through a file and run all the commands inside, so you just need to place the commands in a file, one per line, then add this line to the bottom of the file:
at now + 61 minutes < file
The commands can be any type of one-liner you want to use.

Here is an example. Call this file foo and to kick off the execution the first time, you can simply run: sh foo
date >> ~/foo_out
cd ~/tmp && rm *
at now + 61 minutes < ~/foo
That will output the date and time to ~/foo_out then move to a tmp directory and clean out files, then tell the at command to run itself again in 61 minutes which will again run the at command after executing the rest.

Followers