Cron – Explanation

November 28, 2006 / General Discussion

Cron allows users to automate repetitive system administration tasks such as tape backups, database reorganization, and general file cleanups (such as emptying log files and queues).

The Crontab File’s Syntax
To tell cron what you want it to run, and how often you want it to run it, you need to create a crontab file. A crontab file is just a text file with the following syntax:

Code:

minute hour day-of-month month-of-year day-of-week command

Each of the above columns can be in one of the following formats (these examples are for the minute column):
30
Run the command 30 minutes past the hour.

0-59/10
Run command once every 10 minutes, for the entire hour.

15-30
Run command once every minute, from 15 to 30 minutes past the hour.

0,10,50
Run the command at 0 minutes past the hour, 10 minutes past the hour, and 50 minutes past the hour.
*
Run command once every minute.

And here’s the range of numbers available for each of the time and date columns:
Minute : 0-59
Hour : 0-23
day-of-month : 0-31
month-of-year: 1-12
day-of-week : 0-6
(0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat)

Here’s an example crontab file:

Code:

30 0 * * * ./backup.sh
0,10,50 9-15 * * * ./compute.sh
0-59/10 * * * 1,3,5 ./netgrab.sh
30 0 * * * ./backup.sh

Run the backup.sh script (located in your home directory) at half-past (30) midnight (0), on every day of the month (*), every day of the year (*), and every day of the week (*).
0,10,50 9-15 * * * ./compute.sh

Run the compute.sh script every 0 minutes, 10 minutes, and 50 minutes past the hours (0,10,50), between 9 am and 5 pm (9-15), every day of the year.

0-59/10 * * * 1,3,5 ./netgrab.sh

Run the netgrab.sh script every 10 minutes (0-59/10), every Monday, Wednesday, and Friday (1,3,5).

Creating and Submitting Your Crontab File to Cron

There are two methods to create/modify and submit a crontab file:

Method 1
Create a crontab file in a text editor. You can call it whatever you want and save it wherever you like. Now you have to submit the crontab file. To do this enter:
crontab filename
…replacing filename with the location and name of your crontab file e.g. if the file was named crontab and was in your current directory you would enter:
crontab crontab

Method 2
crontab -e
To open your default editor (which is Vi). When saving the file just enter wq without specifying a Filename; to quit Vi, and automatically submit the crontab file.

Note
When you submit a file, a copy of it is stored in the /var/spool/cron directory, with your username as the filename. So if I submitted the file mycron whilst logged into the account Laurence, a copy would be stored as /var/spool/cron/Laurence. It’s this file the cron daemon uses, and not the master file you created if you used method 1. If you used Method 2 then only one copy of the file exists. Never directly edit files in /var/spool/cron. Cron will not be updated and you can potentially mess things up. Instead use either Method 1 or 2 again, to modify your crontab file.

Listing and Removing Your Crontab File
To display the contents of your crontab file held in the /var/spool/cron directory, enter:
crontab -l
And to remove your crontab file from the /var/spool/cron directory, enter:
crontab -r

Restricting Users
By default, only root can submit and modify a crontab file. To allow users to have their own crontab file add their username to the /etc/cron.allow file, e.g:
john
Julie
Indicating that john and Julie are now permitted to submit their own crontab file.

Putting Theory Into Practice
Here’s a handy little automation exercise for you to try out. It will back up the contents of your home directory to a .tar.gz file each morning at 2.30 a.m.
If you haven’t already done so, enter:
su -c ‘pico /etc/cron.allow’
…and add your username to this file to allow you to submit crontab files.

  1. If you’re not already in your home directory, enter:
    cd
  2. Enter:
    pico backup
    …and enter the following:
    rm backup.tar.gz
    tar cfz backup.tar.gz .
    then press Ctrl+o to save the file, and Ctrl+x to exit.
  3. Now make the script executable by entering:
    chmod +x backup
  4. Enter:
    crontab -e
  5. Press i to enter Insert mode and enter the following:

    30 2 * * * ./backup

  6. Then press Esc to return to Command mode, and enter:
    :wq
  7. …to exit Vi and submit your crontab file. That’s all there is to it. Now you can spend hours thinking of bigger and better ways to make your life as a system administrator an easy one.

    If you don’t have access to cron, consult your local admin. And
    remember, in shell, you can access the cron manual by doing :

    man cron
    Or