Affordable Web Hosting with Excellent Customer Support internet connection free month hosting

Scheduling Tasks Using Cron

The Cron time application server is available in some form or another on all modern Unix systems. Using cron, webmasters can automate many tasks including running scripts, controlling hardware (webcams), updating databases etc.

Cron consists of two main programs:

  • The cron daemon which executes commands and programs at specified times or on specific events.
  • The crontab tool which allows you to create, edit, view and delete Cron tables.

As Cron is standardized, if you are familiar with Vixie Cron, or Dillon's Cron (two popular Cron software packages), you will have no problem using the Cron tools available on your server.

Using Crontab and Manipulating Cron Tables

The crontab tool is used to create, edit, view and delete your Cron tables. The Cron tables are the files that contain the lists of jobs or scripts you want the server to run at certain times or events.

To edit your Cron table, simply type crontab -e at your command prompt. If you haven't set up any tasks, your Cron table will be empty. Here is an example of a Cron table featuring one task:

# 30 minute webcam update
30 * * * * /cam/webcam_upd.pl
						

The hash mark preceding the first line signifies that the line is a comment. The actual command contained in this table is on line 2. Reading this line is pretty easy, here is a breakdown of what each phrase means:

  • 30 - the first characters of the string signify minutes. You can use the numbers 0-59 to have an event occur at that minute of the hour. The command in this example, 30, signifies that the script will be run at 30 minutes of the hour.
  • * - the first "*" represents the hour. You can choose to display the hour in these formats: 0-23, 3am (4PM) or 3:00am (4:00PM).
  • * - the second "*"represents the date. You can display the day of the month using the numbers 1-31.
  • * - the third "*" represents the month. You can display the month using the numbers 1-12, Jan (jan) and January (january).
  • * - the fourth "*" represents the day of the week. You can display the day of the week using 0-6 where Sunday = 0, Monday = 1, Tuesday = 2, etc. You can also use Mon (mon) or Monday (monday) here.
  • /cam/webcam_upd.pl - the last portion of the line specifies which command or script will be run at the time and date you've chosen. In this example, the script "webcam_upd.pl" in the directory /cam would be executed.
  • Note: The hour, date, month and day of the week in this example are asterisks, which means they will be treated as wild cards. Thus the script will not be limited to running at certain hours, on certain days, months, or dates. It will instead run at 30 minutes past the hour regardless of the hour, day, month, or date.

Here are some other examples of Cron table entries:

* */2 * * Mon,Fri /run_every_2_hours_onMF.pl

The "*/2" specifies that this script will run every two hours, and the "Mon,Fri" designates that this will only occur on Mondays and Fridays. It could have also been written as * */2 * * 1,5 /run_every_2_hours_onMF.pl with Monday and Friday being represented by the "1" and "5" respectively.

* 2am */5,1,31 * * /cron/run_every_5_days_and_1st_and_31st.pl

This entry specifies the script will run every 5 days and also on the 1st and 31st on the month.

In addition to the editing mode of crontab, accessed with -e, there are two other switches you can use. To simply display the contents of your Cron table, type crontab -l. To delete your Cron table, use crontab -r.

Using At for One-time Jobs

On most Unix systems, At and Cron work independently but can be used to perform similar tasks. Where Cron is used to perform tasks repeatedly, At is used to run commands or programs just once. For more information on this, please see Using At to Schedule One-time Jobs.

Related Items