Saturday, September 24, 2011

The very useful crontab in linux usage

Linux crontab FAQ: How do I schedule Unix or Linux crontab jobs to run at intervals, like "Every five minutes", "Every ten minutes", "Every half hour", and so on?

I've posted other Unix and Linux crontab tutorials here before (How to edit your Linux crontab file, Example Linux crontab file format), but I've never included a tutorial that covers the "crontab every" options, as in how to run a crontab command every minute, or every hour, or every day.

To that end, here are some quick "crontab every" example entries that show the syntax for these crontab commands.
Linux crontab - run a command every minute

To run a Unix or Linux crontab command every minute, use this crontab syntax:

# run this command every minute of every day to check apache
* * * * * /var/www/devdaily.com/bin/check-apache.sh

I created that entry when I was having a problem with Apache, and needed to run a test every minute of every day to see that it was running properly. All those "*" symbols are what make this command run every minute. Specifically, those first five fields have the following meanings:

# field # meaning allowed values
# ------- ------------ --------------
# 1 minute 0-59
# 2 hour 0-23
# 3 day of month 1-31
# 4 month 1-12 (or names, see below)
# 5 day of week 0-7 (0 or 7 is Sun, or use names)

I'm assuming some previous knowledge of the crontab syntax in this tutorial, so I won't discuss this much, but what I'm trying to show here is that in the first field you specify the "Minute" value, in the second field you specify the "Hour", followed by Day Of Month, then Month, and finally Day Of Week. We'll see more examples as we go along.
Run a crontab command every hour

To run a Linux or Unix crontab every hour of every day, you use a very similar crontab syntax. Here's a crontab entry I use to hit the Drupal cron.php page five minutes after every hour:

# hit this url to run the drupal cron process every hour of every day
# this command will run at 12:05, 1:05, etc.
5 * * * * /usr/bin/wget -O - -q -t 1 http://localhost/cron.php

Run a crontab entry every day

Here's a crontab example that shows how to run a command from the cron daemon once every day. In this command I run my backup scripts at 4:30 a.m. every day:

# run the backup scripts at 4:30am
30 4 * * * /var/www/devdaily.com/bin/create-all-backups.sh

Run a crontab entry every 5 minutes

There are a couple of ways to run a crontab entry every five minutes. First, here's the brute force way:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /var/www/devdaily.com/bin/do-update.sh

That command works just fine, and there's nothing technically wrong with it. It's just that the crontab syntax offers a shortcut for this situation. The crontab step syntax lets you use a crontab entry in the following format to run a Unix or Linux command every five minutes:

# run this crontab entry every 5 minutes
*/5 * * * * /var/www/devdaily.com/bin/do-update.sh

That's a nice convenience feature for situations like this. Here's a nice blurb about the step command syntax from the crontab man page:

Step values can be used in conjunction with ranges. Following a range
with "" specifies skips of the number’s value through the
range.

For example, "0-23/2" can be used in the hours field to specify
command execution every other hour (the alternative in the V7 standard
is "0,2,4,6,8,10,12,14,16,18,20,22").

Steps are also permitted after an asterisk, so if you want to say
"every two hours", just use "*/2".

Unix and Linux "crontab every" summary

I hope that's enough Unix/Linux "crontab every" examples to help you run your own crontab commands every minute, every 5 minutes, or every day, or every five minutes, etc.

As usual, if you have any questions, comments, or crontab every examples to share, just use the comment form below.
Unix and Linux crontab reference information

For more information on the Unix and Linux crontab system, here are two links to the crontab man pages (help/support documentation):

No comments:

Post a Comment