A Simple Introduction to Creating a Cron Job

Cron Jobs
It is common for webmasters to need to set up a cron job. By understanding the basics of creating a cron job, every webmaster will be able to make their lives a little simpler. Here is a basic introduction of creating a cron job that fits your needs.

As a webmaster, you should never be intimidated by the process of creating a cron job. By following simple, step-by-step instructions it is a very easy process that can make your website more efficient and your life much easier.

What is a cron job?

Cron or a cron job is just the name for a program that is created to allow UNIX users to execute command scripts or groups of commands automatically. It is always set to take action based on the chosen time and date. Cron jobs can be a one-time job or a recurring job, it is all up to you. Cron jobs are often used for system admin commands like search a database or running a backup a script. However, it could potentially be used for anything.

Prerequisites

1. The Operating System

In order to use cron jobs, you must ensure that your website is being hosted on a Unix/Linux-type web server. If the web server is using Windows, plese check out Simple Ways to Set Up a Cron Job on a Windows Server.

2. Shell Access / Control Panel Interface to Crontab

In order to set up a cron job, you must be able to connect to your host using telnet or SSH. If this is not possible, then your host must give you a way to set up crontab tasks using the control panel.

Field Values

It is important to understand that cron jobs are set up and run on time intervals that you specify In order to specify these intervals, you must know what each field stands for. In total there are 5 time fields.

  • 1. Minute – This controls what minute of the hour, the cron job is executed (0-59)
  • 2. Hour – This specifies what hour the command will be run on and operates on a 24-hour clock (0-23; 0 = Midnight)
  • 3. Dom (Day of the Month) - This controls what day of the month the cron job is executed and is based on a 31 day range (1-31)
  • 4. Month – This specifies what month the cron job will be executed (1-12 or the name of the month)
  • 5. Dow (Day of the Week) – This is the day of the week that the cron job will run on (0-7 or the name of the day; also 0 and 7 both equal Sunday)

To gain an understanding of these fields here are a few examples (If you do not want to use the field, then use * in place of the value):

  • 03 * * * * = The command will run three minute past every hour
  • 03 8 * * * = The command will run every day at 8:03 am (for 8:03 pm, replace the 8 with 20)
  • 03 8 * * 0 = This command will run at 8:03 am every Sunday (you can also use “sun” in place of the 0)
  • 03 8 2 * * = This command will run at 8:03 am on the 2nd of every month
  • 03 * 2 06 * = This command will run hourly on the 2nd of June

These fields must always be completely filled in and will always be in the same order!

Additional Helpful Notes:

  • 1. If the DOM and DOW are both specified, then the command will be executed when either of the times are at hand. For Example: 03 8 12 * Sun = will run at 8:03 am every Sunday and the 12th of every month.
  • 2. In order to shorten the amount of cron jobs that you need to create, you can compile several jobs into a list (1,2,3 and 1-3 both mean “1, 2, and 3”. For Example: 03 8 * * 1,2,3,4,5 = the job will run at 8:03 am on Monday, Tuesday, Wednesday, and so on.
  • 3. Cron job also recognize “step values”. For Example: The value of */3 in the DOM field would be that the command would be executed every 3 days

Following the 5 fields that delegate the time the cron job is executed is where you tell the job what command to execute. This field is known as the command-line-to-execute portion.

For example, you have a Perl script named “Test.pl” and you want this script to be executed every day at 8:03 am, the complete line would like:

“03 8 * * * /your/directory/test.pl”

If your script must be pulled directly from your web browser and its name was “test.php” then it would look like:

“03 8 * * * /usr/bin/wget http://www.yourdomain.com/test.php

Recap

At this point you should know: 1. What Your Prerequisites Are 2. How to Manipulate Field Value and 3. Additional Helpful Notes.

By understanding only these basic facts, you will be able to use and manipulate many of the most common cron jobs. However, as with everything regarding computers, the internet, and websites, it can always become more complex. Never be worried, there are always additional resources that can help you with more complex issues.

Subscribe to WebmasterFormat RSS Feed Follow WebmasterFormat on Twitter
hot wheels:

Cron isn't working

My script set up too be cron trigered isn't running. How could I verify cron is working?

Anonymous (not verified):

Try running it manually

Try running it manually and/or set up another script to be run by cron.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <blockquote> <b> <center>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.