This article aims at explaining to novice what a cron job is, so if you know what I mean, you don’t need to read this article because you will not learn anything!
Principle and interest of cron jobs
A cron job is a program that is triggered at a specific time automatically by the server, so this is the equivalent of the Windows Scheduled Tasks for web servers.
Examples of scheduled tasks: weekly anti virus checkup every Monday at 12pm, nightly backup at 1am, or send an email reminder every 1st of the month.
The interest of the cron job is the automatic triggering of a generally periodic action, without disturbing web site itself.
Take the example of sending a reminder email every 1st of the month to a base of 500 contacts. It would be possible for the server to verify, each display of a web page, if we are the first of the month. When we are actually the first of the month and the emails have not been sent, the server would send 500 emails and would display the page.
But what a waste of performance : for 30 days, display pages will be (slightly) slowed by checking the date and strongly when sending 500 emails.
Thus, developers are resorting to planned tasks by concerns of performance.
FYI, the term cron job comes from the name of the program which triggers the scheduled tasks on Linux / Unix (Wikipedia).
I understand, but how to program a scheduled task on my server?
Unfortunately, it depends on accommodations : shared or dedicated server, as well as services offered by your hosting company.
1 – Locate the program to start
First, you’ll need to know the program to start, and more specifically its URI (or URL).
Depending the functioning of your task scheduler on your server, you will need :
- URI = path on the server to the script to be triggered. The URI does not begin with http://. Example of URI : /home/www/modules/mymodule/cron-script.php.
More rarely, the task scheduler uses the URL :
- URL = the address of the script page that begins with http:// Example : http://www.mywebsite.com/modules/monmodule/cron-script.php. In general, such information is provided to you by the publisher of the program you need to schedule.
2 – Add the program to the planner (crontab)
“Industrial” hosting company typically provide you with a server management interface, which includes the ability to schedule cron tasks. If you don’t have access to such an interface, you will nee to contact your webmaster.
OVH : simple interface. Your manager follow “Accommodation” and “Task Scheduled”. Guide.
Gandi : you must add a code line in a file. Guide.
1&1 : you need to run a command line. Guide.
3 – Verify proper execution
Hehe, this is definitely the most difficult part: how to know if your cron job work? For example, if you do not receive an email that your scheduled task is supposed to send, how to know if the problem is due to the non-execution of cron job or php does not work.
Again, this will depend on your hosting company.
With OVH, you may receive an email each time the cron job (the first 10 executions).
With Gandi, you have access to a log with all cron jobs (Administration of your instance> Crons> Logs).
Some modules include a log indicating the dates of the last executions, for example:
If you are sure that your job is actually executed, but the expected result does not occur, it is likely that the problem lies in the module, contact the module seller!