Creating Cron Jobs

By Ollie Armstrong Thursday, 3rd March 2011

Welcome to a short getting started guide into scheduling regular tasks using cron.

Unless you have a reasonable background with Linux systems you probably haven’t got a clue what I’m on about when I mention cron. Let’s start basic. Say you want to run a command at a specific time of day, everyday, like clearing the temporary directory, for that we use cron. Cron is a method of scheduling tasks and it’s very versatile.

To be able to use cron we have to use what is called a crontab, short for cron table. It’s the method used to schedule the commands and specify when they run.

Getting Started

For this you are going to need fire up the SSH connection (refer to Getting to grips with SSH Part:2 if you need to know how to do that). Now, what we are going to do is use the crontab command to do various things like viewing and editing the crontab.

crontab (-e -l -r)

You just need to add one of the switches to the end (either ‘-e’ ‘-l’ or ‘-r’, there are a couple more options, but we don’t need them). The first command I want you to try is

crontab -l

This is the command to print the cron table to the screen. As we haven’t used crontabs before you will get a message like this, quite simply telling you there isn’t a crontab yet.

All we need to do to create the crontab is run the edit command and it will automatically create the file for us. So now run

crontab -e

This will create the crontab file, and open it up in the vi text editor. If you’ve forgotten how to use it, refer to my lovely guide SSH: Vim Editor.

Extra tip: If you are more comfortable using the nano editor (I haven’t covered it on a guide but you may prefer it) then first run the command: export EDITOR=nano and that will set the default editor to nano, so running crontab -e will open it in nano. However, I will be using vim in all my examples.

Using the Crontab File

When using the crontab file, each task we wish to schedule resides on a different line. So first rule: new line for new task. Right, now each line is made up of 6 individual parts. A crontab line will look something like this:

00 12 * * 0 echo "This is run at 12:00 pm, every Sunday" >> ~/cron.log

See if you can see each of the parts. There are 5 sections at the start for numbers, and then the command goes at the end. In the example, the command is everything after (and including) echo, but the numbers before are explained here.

Minutes Hours Day of Month Month Day of Week
00 12 * * 0
Values: 0-59 Values: 0-23 Values: 1-31 Values: 1-12 Values: 0-6 (0 is Sunday)

You can replace any of the numbers with a wildcard (the asterisk symbol) and that will ignore the certain field. The rest should be self explanatory, except maybe the command I’ve used. Basically it will print that text to a file called cron.log saved in your home directory.

Creating a Task

Let’s go for the traditional route and create a scheduled task that will save the words “Hello World” to a file at a particular time. But to save waiting for ages, we’ll just pick a time a couple minutes in the future. Type this into your open crontab file and save it. (Obviously change the time to something more appropriate to when you are doing this, remember, minutes first then hours.)

(The ‘$(date)’ bit just echoes the date and time into the string)

Quick hint: If you run the date command from the Terminal window, you will see the exact time on the server, then you can pick a time a couple minutes ahead of that.

After you have saved it, exit out of vim, then you just need to wait until the time you specified in the crontab and then check the file ~/cron.log. Do this by running

cat ~/cron.log

And you should see something like this

Expanding

Now you know the basics you can schedule just about any command to run. For example you may have a directory you want to clean every week, or the first of every month. Or maybe you want to print a list of every file in a directory daily to keep an eye on everything that’s in there. It’s up to you what you do, the possibilities are endless!


Posted in Advanced Configuration, Guides | No Comments » twitter-follow facebook-follow rss-follow

Leave a Reply

Your email address will not be published. Required fields are marked *