Sometimes when running a website you might need to run scheduled tasks at certain times. Certain software may require you to set up these tasks so they can work correctly. For example, you may need to update or backup your database on a daily or weekly basis, or send a notification email.
For these types of scheduled task, you can set up a cron job to handle them.
What is a cron job?
There's a bit of jargon to cron jobs, so let's go through exactly what they are and how they work:
The software utility cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs to run periodically at fixed times, dates, or intervals.
Put simply, a program on the web server (known as a cron daemon) runs in the background and is responsible for launching your cron jobs when they are scheduled to be run.
This schedule is stored in a configuration file named
crontab where all of your tasks and their times are listed. This step-by-step guide will help you to schedule your tasks using this crontab program.
What you need to schedule a job
There are a couple of things you'll need to be able to create a cronjob for your website:
- A unix/Linux based operating system. Cron and crontab are command line functions which only work with Unix-like operating systems such as Linux. If you're using a Windows server, you won't be able to do cron jobs. Instead, you'll need to use the Windows Task Scheduler.
- Secure shell access or control panel interface for Cron. You need to be able to access your site or server via the command line with shell (SSH) access or alternatively, your web host needs to have a Cron scheduling app in their control panel dashboard.
This tutorial assumes that you have shell access to create your cron jobs. Even if your hosting company offers a tool to set these tasks for you, it's useful to lean how to do it yourself. It's very easy once you get started.
If your web hosting provider doesn't offer SSH, my cheap web hosting guide might help you find one that does.
Setting up your cron job on the server
- Choosing your schedule for the task. Before you start you'll need to work out how often your cron job task should run. Some shared web hosts have set limits on this and might only allow you to run a task once every 15 minutes or more. If you have a VPS or your own server, usually you have no limits on how frequent your tasks can be.
- Learning how to write your script for crontab. The syntax for crontab is a little bit confusing at first, but not too difficult once you understand it. The basic format of a crontab schedule has 6 fields which are separated by spaces. Here's what each field means:
minute hour day month day-of-week command-line-to-execute
Each field has an accepted value range, and they must be in that order with no empty or missing fields. The accepted values for each are:
Field Accepted value minute 0-59 hour 0-23 day 1-31 month 1-12 day-of-week 0-6 command-to-execute the command to run, and any parameters it needs
The asterisk is often used in the cron job and crontab syntax as a wildcard. It means all possible numbers for that position. For example, setting the minute as * would mean the cron job is set to run every minute.
Cron job examples
This cron job will run every minute of every hour of every day (and might not work if you run shared hosting that restrict cron jobs):
* * * * * your-command
This cron job will run at minute 0 of every hour and is a common hourly task:
0 * * * * your-command
This job will also run hourly, but at 30 minutes past the hour instead:
30 * * * * your-command
This cron job will run once per day at 5.40pm (note the use of 24 hour time):
40 17 * * * your-command
This job will run every Friday, every hour (but not on any other day of the week):
0 * * * 5 your-command
You can use multiple numbers separated with a comma. This will run at 15, 30, and 45 minutes of every hour, every day:
15,30,45 * * * * your-command
You can also the division operator. This will run 10 times every hour (every 6 minutes):
*/6 * * * * your-command
Specify a number range using the dash symbol. This will run once per hour between 01:15am and 04:15pm:
15 1-4 * * * your-command
You can also run a cron job every time the server reboots:
Editing the crontab
Once you have your script and your cron job ready, you'll need to add it to the crontab so that it can be scheduled and run on the server. To view the crontab you can use the follow command when connected to your server using SSH:
This will open up the crontab file in the vi text editor used with Unix based operating systems. It can be difficult for beginners to understand and use though. If you decide to edit the crontab this way, you may need to learn some vi commands.
To see the crontab without actually editing it you can use:
If you want to remove the contents of your crontab, you can use this command:
Creating your crontab file
The easiest way for beginners to create a cron job and add it to crontab is to use the examples above, along with your script, and create a simple text file with all of your jobs – each on a separate line. You save this file using whatever name you like and then you'll need to upload it to your server using an FTP client such as FileZilla.
To use this text file as your crontab, you can use the following command (you may need to “cd” or change directory to find where you uploaded your text file):
Doing this will overwrite all existing cron jobs without warning, so make sure you've either backed them up or included them in your text file if you need them.
Email notifications for your cron jobs
By default, cron will send any output from the script to your email address if you've specified one. If you have multiple cron jobs running and you don't change the default output, you'll receive everything the script outputs – which can be overwhelming for your email inbox.
To add email notifications to your crontab schedule, you'll need to modify it by adding
MAILTO="" to a new line. For example:
MAILTO="email@example.com" 0 * * * * your/cron/job.php
This will send email notifications each time this script is run, complete with outputs. For this job, that means an email every hour for each time the script is run.
Receiving error notifications
If you don't want to receive the entire output of your tasks, but you do want to be notified of errors in the script then you can redirect normal output messages into a “black hole” by using the command
> /dev/null. Anything sent here is ignored by the system, but error messages will send as normal. For example:
MAILTO="firstname.lastname@example.org" 0 * * * * your/cron/job.php > /dev/null
Common cron job errors
You you received any error messages when running the command
crontab my-cron-jobs.txt then you may have made one of the following errors:
- You split your time schedule and script command onto more than one line. All cron job tasks should be a single line. Make sure you turn off the Word Wrap function of any text editor you use and avoid Microsoft Word or other word processors that add formatting.
- Your crontab file didn't end with a new line. At the end of your last crontab command line, you need to press enter or return to end your file with a new line.
- Your text file was not in ASCII format. If you used Word, or added formatting this may have caused the error. Resave your file as .txt using a simple editor like Notepad then reupload the .txt using your FTP client.
Cron jobs can seem overwhelming at first, but once you've run a few of them with various scripts you'll see that they are a valuable tool for system admins and performing many functions on web sites and web applications.