The Basics of CRON and Linux Automation
by Mark Rais, senior editor reallylinux.com.


Readers of this article also chose to view: The Essentials for Using Linux FTP 

This beginner introduction to the Linux/Unix CRON process and tables allows even a novice to automate commands that need to be run repeatedly.

The benefits for automating tasks and commands is obvious. System administration becomes far easier, and you can perform multiple tasks or maintenance without having to be present.

Now, let's get started with the basics of cron and at.

ONE TIME AUTOMATED TASK
If you have no need for a command to be run repeatedly, then it will save you a whole lot of work to just use the at command.

For example, running as root you type:

at midnight
Once you press enter, you are taken to the at command prompt. Beside the at> prompt type your command. For example:
at> who > who.out
You are allowed one command per line. Press enter to go to the next line. When done, press the Ctrl and D keys simultaneously.

You should now see:
X             2006-05-26 00:00 a yourname
The X represents the number assigned to your at command.
What you successfully created was an automated process that will run your command tonight at midnight. Now you can leave work, go home and get some sleep while your Linux server handles your tasks.

If you decide, prior to midnight, to delete this command, then simply type:
atrm X
Replace the X with the number of your command. Note that the atrm is the at remove command.

You can set lots of different times for at. The easiest way is to simply type: morning, noon, teatime, or midnight. Yes, "teatime" really works and runs a job at 4pm!

But what if you need to run a command consistently during the month or week? Read on.


THE USER
Cron is a tool that functions based on a specific Linux user. In fact, every user on a server can have individual cron settings.

Therefore, whoever you are logged in as will be the one whose cron settings you create. If you must create a cron setting that root user initiates, you should be logged in as root.


THE SERVICE
Before you can modify cron, you must ensure the cron process is actually running on your system.

Please read my article Starting Processes in Fedora, SuSe, Ubuntu, for much more detail and help on this step.


UNDERSTAND CRON SYNTAX
To create a specific cron setting you need to understand a bit about the way a command is written for cron. The best way to help you understand is to show you some examples.

First, always remember that a cron entry is specified in the following sequence:
Minute (0-59)  Hour (0-23)  Day of Month (1-31)  Month (1-12 or Jan-Dec)  Day of Week (0-6 or Sun-Sat)  Command
You do not need to specify each item. If you want to specify every month you simply use a * to denote every occurance. Using this same sequence above, and the option of * for every occurance, here is a simple example:
* * * * 5 who
The above line will run the who command every minute, of every hour, of every month, for each Friday. The 5 being in place of Day of Week.
5 * * * * who
The above line will run the who command on the fifth minute, of every hour, of every month, for all days of the week. The 5 being in place of Minute.

What if you want a command to run every day at midnight?
0 0 * * * who
The above line will run the who command at midnight, of every month, for all days of the week. The 0 is in place of Hour and minutes, and represents midnight (0-23 of a 24 hour clock).

One last feature that allows even more flexibility is the use of the / (that's slash). For instance:
*/15 * * * * who
The above line will run the who command every 15th minute, of every hour, of every month, for all days of the week. The /15 being in place of frequency of Minutes.

There are many more options but in hopes of keeping things simple, let's move ahead.


CREATE AN AUTOMATED TASK
You're now ready to create an automated task.

To access the cron process and add a specific command for automation you use a tool referred to as crontab.

This command must be typed in at a command prompt, also called a Terminal prompt. You can get to your command prompt usually by going to your main menu choosing System Tools, then Terminal.

Type the following items in the command/terminal:

1. Start the crontab tool
crontab -e
2. You will be switched to an unusual looking interface, also called the Vi Editor. Press the i key to Insert your cron command. 3. Now type the following:
*/15 * * * * yourcommand
Please replace yourcommand with an actual command line you wish to run.

4. Now press the Esc key.

5. Type exactly the following and press enter when done:
:wq 
You should see this:
crontab: installing new crontab
You have just setup a new cron function that will run a command every fifteenth minute of every hour of every day.

Wait, maybe you didn't want to do that! Maybe you wish to run a different command?

To DELETE your crontab entries simply type this at the command prompt:
crontab -r

REAL WORLD EXAMPLE
I like to offer a real world example of adding a cron entry. Many people are now using a web content tool called Moodle for their organization or educational institution. The Moodle tool requires running a key process every few minutes.

Running basic commands every few minutes on a Linux server rarely reduces performance. However, keeping in mind that running a process too frequently may cause some slowdown, I set my crontab to run every 10th minute.

In my case the cron needs to be created to allow a Moodle website tool to run every ten minutes of every hour. Here is the command:
*/10 * * * * wget -q -O /dev/null http://example.com/moodle/admin/cron.php
Here is exactly every item that I typed:
crontab -e

i
*/10 * * * * wget -q -O /dev/null http://example.com/moodle/admin/cron.php

:wq

crontab -l
I used the crontab -l command to list the entry to ensure it was included in the right way.

That's all there is to it. I hope you will find this information beneficial to setting your own automated processes.

Mark Rais serves as senior editor for reallylinux.com, promotes Open Source to organizations and government leadership in USA, Asia and Africa, and has written numerous Linux books, including Linux for the Rest of Us.

Linux is a registered trademark of Linus Torvalds. IBM, PC-DOS, and OS/2 are the registered trademarks or trademarks of International Business Machines. Microsoft, Microsoft Vista, Microsoft Windows are all trademarks or registered trademarks of Microsoft Corporation both in the United States and Internationally. All other trademarks or registered trademarks in this opinion piece belong to their respective owners.