How to Automate Functions on Your Website with cron Jobs
There are times when you need to do various different things on a set schedule and the web host server can help you with that using what is known as the cron.
Think of cron as being short for chronology, and it will help you see that it is for jobs that are performed automatically on a fixed time schedule by the server's internal clock. It’s like setting the timer to switch the heating on before you get up in the morning or setting your DVR to record the same show every week.
The command for a cron job basically has two parts:
The command that the server should run: This means giving it the full path to the file that you need to run and any switches that are needed to perform the function. The software you run on your website should provide the correct switches to include.
The exact time and schedule on which the job should be executed: There is huge flexibility built into this system, meaning you can schedule the task to run exactly when you want on whatever schedule you want.
cPanel provides an easy configuration screen for cron jobs. Here’s how to use it:
Open cPanel and log in using the details your host gave you when you signed up.
Scroll to the Advanced section and click Cron Jobs.
If you want to be e-mailed every time any cron job runs, enter your e-mail address in the first box and click Update Email.
Select the schedule on which you wish the command to run.
There are some common settings to select from, or you can create your own schedule.
You must fill every box when setting the schedule; otherwise the cron job creation will fail.
Enter the command you want the server to run into the Command box.
You must add the full server path to the file you want to run in the cron job. The server cannot guess which folder the file might be in!
Click Add New Cron Job to create the job and add it to the cron.
It may take you a little while to get used to how cron jobs are scheduled because different characters do different things. Here is an explanation of them:
Numbers 0-59: Which numbers you can use depends on whether you’re setting the minute, hour, day, month, or weekday. There are not 59 hours in a day, so you can’t use 59 as an option for hours, but you can use any valid number for that time period. So if you want something to run at one minute past the hour, you put a 1 in the minute box.
You can select for it to run at multiple different minutes by separating the minutes with a comma. For example, if you wanted it to run at 1, 5, 27, and 50 minutes past the hour for some reason, you would simply input 1,5,27,50 into the Minute box.
Asterisk (*): An asterisk means every, so putting an asterisk in the Hour box means the cron job runs every hour. Putting an asterisk in the Day box means the job runs every day, and so on.
Slash (/): You can use a slash after an asterisk to divide that every by a number. A slash always has to have a number following it. For example, if you want the job to run every month, you simply put an asterisk in the Month box.
If you want it to run every three months, though, you have to type */4 — which means every month divided by 4 (or 12/4), which equals every three months. It can be a little confusing, but the cron job screen has Common Settings drop-down boxes that help explain what to do.
Dash (-): You can use a dash only in the Weekday field. The Weekday field is a special field that modifies the Day field. For instance, if you want to run the command only on the weekends, you type (0, 6) because 0 stands for Sunday and 6 stands for Saturday.
You can use the dash to signify every day between two days, so for example, 1-5 signifies every day from one to five (Monday through Friday). Using 3-6 would mean Wednesday through Saturday.
You can edit or delete cron jobs at any time after you create them. Any change or deletion is immediate and stops the previously next scheduled run and replaces it with whatever you change the settings to.
You may find cron jobs extremely useful and their scheduling system is amazingly flexible, allowing you to schedule the server to run any job you want at pretty much any minute of the year you want it to.