Using cron to run programs on a schedule

December 20, 2009 by: Allen Sanford

This guide is for the newbie more than the experienced Linux Pro, and maybe some of the users in between will also find it useful.

So you need to automate a task to run at preset intervals, well cron is what you are looking for.
cron is a Linux system process designed to execute a program/script at a preset interval of your determination. In order for cron to run what ever program/script you need, you have to configure a few things first. There are two options: you can use the system crontab file or you can use your user crontab file. What is the difference you ask, simple if you use the user based crontab file it will execute your script/program as that user, assuming that user has the proper permissions, if you use the systems crontab it will run the script/program as root. Then you use the crontab program to load the text file that describes the cron jobs into cron. or in the case of the system cron you don’t have reload anything it happens automagic.

The first thing you need to understand is the format for the setting a cron job in the crontab. Below is the syntax used to configure a new cron job.

    [min] [hour] [day of month] [month] [day of week] [program to be run]

And now a brief explanation of each optionis as follows:
[min] Minutes that program should be executed on. 0-59. Do not set as * or the program will be run once a minute.
[hour] Hour that program should be executed on. 0-23. * for every hour.
[day of month] Day of the month that process should be executed on. 1-31. * for every day.
[month] Month that program whould be executed on. 1-12 * for every month.
[day of week] Day of the week. 0-6 where Sunday = 0, Monday = 1, …., Saturday = 6. * for every day of the week.
[program] Program to be executed. Include full path information.

Now for some examples:

    0,15,30,45 * * * * /usr/bin/foo
    */15 * * * * /usr/bin/foo

Both of the above are the same and will run /usr/bin/foo every 15 minutes on every hour, every day-of-month, every month, and every day-of-week indefinitely . In other words, it will run every 15 minutes for as long as the machine is running.

    10 3 * * * /usr/bin/foo

The above will run /usr/bin/foo at 3:10am every day indefinitely.

    10 * 1 * * /usr/bin/foo

The above will run /usr/bin/foo at 12:10am on the first day of every month.
Now remember the first example above where I told you that 0,15,30,45 is the same as */15 well here is another very useful senario lets say you need to run something every 3 days well here is how you would do it.

    16 * */3 * * /usr/bin/foo
    10 14 * * 1 /usr/bin/foo

The above will run /usr/bin/foo at 2:10pm on every Monday.

For more options and more in depth technical mumbo jumbo you can see the man page by running

    man crontab -S 5.

Now that you know a little about how to use crontabs lets talk about the two different senarios that you could possible be using cron.

Scenario #1 (User crons)
The `crontab` command is used to load cron jobs into cron from a text file that contains all your cron jobs formatted like we described above, one per line. First create a text file that uses the above rules to describe what ever cron job that you want to load into cron. However, before you load it, type the following to see if any pre-existing crons are in your crontab.

crontab -l

This will list any jobs that are currently loaded in crontab. If none are listed, then it is safe to load your new cron file without consideration for any existing crons. If there are rules listed there you will need to add them to your cron file (Copy and paste works great but so does `>> crontab.txt`). Now lets me show you what to do to create and load a brand new set of cron jobs into your crontab. Create a file somewhere in you home directory named `crontab.txt` with your cron kobs in it

?Download crontab.txt
    10 3 * * * /usr/bin/foo

Now to load your new file into crontab run the follwing command.

    crontab crontab.txt

Check to see if it was loaded by typing the folowing again.

    crontab -l

It should display something like this:

    # DO NOT EDIT THIS FILE - edit the master and reinstall.
    <a class="zem_slink" href="http://en.wikipedia.org/wiki/Ellipsis" title="Ellipsis" rel="wikipedia">...
    ...</a>
    10 3 * * * /usr/bin/foo

Scenario #2 (System crons and the easier method)

    vim /etc/crontab

Edit and close and the new crons will run by “automagic”. You will notice some lines in there running other scripts located in `/etc/cron.*` where * could be monthly daily, or other naming conventions depending on what system you are on.

See man cron, man crontab and man crontab -S 5 for more information.

Enjoy and Have a Good’n!

Reblog this post [with Zemanta]

Filed under: Linux
Tags: ,

Leave a Reply