Cron for dummies

Written by all for wordpress on 11:24 AM

What is Cron?

Cron is a program that enables you to execute a command, or a script with a sequence of commands, at a specified date, time or at set intervals. The commands or scripts that you want cron to run are defined in a file called crontab, and every user has their own independent crontab file. Cron is a system program that is running all the time and is similar to the Windows scheduler which allows you to run commands/programs at predefined times and intervals.

This tutorial will explain how to use Cron and crontab and contains some basic working examples that should hopefully illustrate how it functions.

Note: Cron is only available on the CGI server. It is not possible to run scheduled commands or scripts on the homepages (www) server.

How do I use Cron?

First, to use Cron you need to make sure that your CGI service is enabled, and additionally you know how to connect to the CGI server using a Telnet or SSH client. For full details of this process, you should refer to the CGI/Shell Server Basics tutorial.

Cron tasks are specified in a file called "crontab", which you edit using the command crontab -e. Whatever you type is prefaced by username@shellx username $.

username@shellx username $ crontab -e

This command, by default launches a program called vi. Instructions on using vi are provided in the CGI/Shell Server Advanced Topics tutorial.

You will see an empty window appearing on screen, this is where you can add the commands you wish executed through cron.

There is a special format for entering crontabs:
Minute Hour Day Month Day Task

Minute = Minute of the hour, 00 to 59. * Will indicate every minute (details later)
Hour = Hour of the day in 24-hour format, 00 to 23. * Will indicate every hour (details later)
Day = Day of the month, 1 to 31. * Will indicate every day (details later)
Month = Month of the year, 1 to 12. * Will indicate every month (details later)
Day = Day of the week, 3 chars - sun, mon, tue, or numeric (0=sun, 1=mon etc).... * Will indicate every day (details later)
Task = The command you want to execute

Note: each of the above must be separated by at least 1 space.

It is advised to include the following line, or similar at the top of the file:

MAILTO=cron@username.plus.com

This ensures any error output from the cron tasks and any output from your script gets emailed to an address you can pick it up from. If you do not add this entry, any errors our script output will be appended to a file called Mailbox in your CGI user account home directory. This insures that you can easily find and resolve any problems that occur when running a command or script through cron. To stop any email or Mailbox file being generated when a command is executed, use MAILTO="". This should only be used once you have established the commands specified in your crontab file are working correctly.

absolute pathnames

When entering commands into the crontab file, it is important that you use absolute pathnames (such as "/files/home1/username/script.php") to specify the script in crontab. This is also true for any local scripts you use within other scripts and for any system commands you use within the script you are running. This is because when cron runs your command, it does not have the same $PATH defined for finding system commands. To find the path to a system command, just enter whereis command at the $ prompt and you will get a path to the command returned, which you can then use within your script. One of the common failures is not using an absolute path when run from cron. Even though it runs perfectly when you run it from the $ prompt.

You can use the environment variable $HOME to simplify the path to files and scripts within your home directory as it corresponds to the full path to your home directory. So instead of using /files/home1/username/php/script.php you can use $HOME/php/script.php.

So, an example crontab may look like:

MAILTO=cron@username.plus.com
* * * * * /command/to/execute


This would execute /command/to/execute every minute.

Now that you have a basic understanding of how cron works, we will expand on it with some examples.

Examples

How do I run a task every 5 minutes?

One option is to use

MAILTO=cron@username.plus.com
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /command/to/execute


However, there is a special shortcut for this:
MAILTO=cron@username.plus.com
*/5 * * * * /command/to/execute


The */5 is known as a short form equivalent to 0,5,10,15,20 etc... and achieves the same effect as the previous example, executing the command every 5 minutes. Other examples are: */2 would be every 2 mins, */30 every 30 minutes and so on. You can use the same short form for the hour indicator */2 every 2 hours, */6 every 6 hours etc.

How do I run a task at 6PM every night?

MAILTO=cron@username.plus.com
00 18 * * * /command/to/execute


How do I run a php script at 2am every Sunday?

MAILTO=cron@username.plus.com
00 02 * * sun /usr/local/bin/php $HOME/php/script.php


Notice that the php command is specified using an absolute path because cron will not be able to find it otherwise. If it was not specified, the script.php will fail to execute and an error like: php not found will be reported in the email you receive or in Mailbox. You may not spot this, especially if you run it successfully from the $ prompt as php /absolute/path/to/script.php or even php script.php if it is in the current directory. Also note $HOME is being used instead of /files/homeX/username/

Example MailBox or Email from cron

tutorialsteam@shell2 tutorialsteam $ more Mailbox
From root@shell1.cgi.plus.net Sun Feb 01 23:11:27 2004
Return-Path: <root@shell1.cgi.plus.net>
Delivered-To: tutorialsteam@shell1.cgi.plus.net
Received: (qmail 18527 invoked by uid 10667); 1 Feb 2004 23:11:25 -0000
Date: 1 Feb 2004 23:11:25 -0000
Message-ID: <20040201231125.18514.qmail@shell1.cgi.plus.net>
From: root@shell1.cgi.plus.net (Cron Daemon)
To: tutorialsteam@shell1.cgi.plus.net
Subject: Cron <tutorialsteam@shell2> $HOME/me
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/files/home2/tutorialsteam>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=tutorialsteam>


I am:
uid=10667(tutorialsteam) gid=500(shellcgi) groups=500(shellcgi)
PATH is set to:
/usr/bin:/bin

Crontab command options

crontab -e

As explained earlier, this will allow you to edit the contents of your crontab file or create a new crontab file if one does not already exist. The editor used is called vi or vim.
crontab -l

This will list the current contents of your crontab file and is very useful for checking you have edited it correctly after crontab -e. It is often useful to make a copy of the crontab file in case you make a mistake with an edit.
$ crontab -l >mycrontab

This will create a local copy of the crontab file called mycrontab.
crontab -r

Use with caution: This will delete the contents of your current crontab file (another reason for making a local copy!)
crontab file

This is an alternative method for setting up your crontab file. Instead of using crontab -e, you can create a file containing the cron commands and use that to replace or overwrite the current contents of your crontab file. Note replace - it will overwrite anything that is currently in your crontab file with the contents of file.

Related Posts by Categories



Widget by Hoctro | Jack Book
  1. 4 comments: Responses to “ Cron for dummies ”

  2. By Anonymous on April 4, 2008 at 12:20 PM

    1) If you don’t have an account already, register on PayPal ( www.paypal.com ). Its free.

    2) www.AWSurveys.com/HomeMain.cfm?RefID=mirbabu

    also free.

    3) Log in AWSurvey click “Browse Ads”. Click the listed advertisements and wait until the 30-second counter finishes before closing the window. Click each Ad ONE AT A TIME. It won’t work if you just open all ads at once.
    You earn money on this site by clicking ads.

    4) When you have earned 75 dollars, transfer them to your PayPal account by clicking “Cashout” in your “Members” window.Now you can buy rapidshare premium account.

    5) Don’t forget to come back everyday and click more ads to get more money

    Leave a Reply

  3. By Huntly Cameron on June 14, 2010 at 3:04 PM

    Hey, cheers for this. Very helpful! Everything else I started looking at dropped you in at the deep end with bricks attached to ones feet ;)

  4. By AttilaNab on September 25, 2012 at 1:53 AM

    Hello. It was really informative i was also having the same issue but Long Path Tool helped me in this situation. You can see here PathTooDeep.com. It might help you as well.
    Thanks and Regards,
    Attila

  5. By jecriley on April 12, 2017 at 1:20 PM

    I just wanted to leave a comment that wasn't spam...

    Very helpful. I found this page because it has been quite some time since I used a Linux box, so I completely forgot how to close the edit screen after crontab -e and get it to save the changes. While this document did not answer this question directly, it did point me to looking at vi.

    So.... if you ended up here for the same reasons I did... hit the "insert key" to edit, "esc key" to quit the edit and type ":exit" to quit the editor. crontab -l should now show your changes.