First – what is a cron job? It is a piece of software which can be run at specific times by the cron service. By example, one could set a cron job to update his application exchange rates from a server every day at a specific hour so its application will be up to date.
I used it because the Romanian National Bank doesn’t have a proper page to check for exchange rates – they present just the numbers, but no trends, no charts – nothing visual. Too bad for them! For me it is actually a good way to refresh my PHP / MySQL and apache management skills. Because this Cron stuff is actually new for me, I composed a small tutorial in order to be easy for me to remember and for you, my dear readers, to get quickly the information required for a quick start. So let’s get going with our ..
Cron Jobs quick Tutorial
There are many ways to access the cron service´, but for conveninence I will present the cPanel alternative. I am sure that the guys that are confortable in command line won’t need my tutorial. And for the rest of us, point and click in a nice interface is everything. To access the Cron Jobs Menu, click on the corresponding icon located on the main screen of your cPanel interface. Usually, it can be found in Advanced tab, in the middle. Or you can search it with the Find function in the left hand column.
After pressing the link of the Cron Jobs menu you’ll be redirected to a window, which appears as follows:
So we are offered two different modes that we can use to add a cron job: Standard and Advanced (UNIX Style). The difference between them will be presented below. From my point of view, the difference isn’t too big once you’ve setup and tested for the first time a cron job. The interface in both areas is clean and easy to understand.
The easy way: Standard Cron Manager
The Standard mode provides a range of pre-set options that you can choose. This is the simplest method and is recommended.
The steps required to add a cron job in standard mode:
- First access the Cron Jobs Menu by clicking on the icon located on the cPanel interface main screen.
- Choose and click on Standard link.
- As a good practice, enter an email address to which the cron output will be sent in the corresponding field.
- The tricky step – Enter the command you wish the cron job to run in the blank field next to the ‘Command to run’ label. I say tricky because this field isn’t documented and yet it is the most important one. Basically what’s inputed here will be sent as parameter to cron. So first you have to specify what will run – in our case bash, but can be php and after this the script that you want to run.
- Select how often you wish the command to be run by selecting an option in each of the boxes – pretty self descriptive so I won’t get into details. If you want it to run for one hour at five minutes interval at 17 oclock every day, each month you’ll get the settings correctly.
- The only thing that remains is to click on Save Crontab to save your changes and set up your cron job.
The command run by the cron job must be a valid command, so it is better to have it tested for a while not to have surprises.
Removing from list a cron job
At yome point you’ll have few cron jobs that you no longer need, so you should delete them so the command will not continue to run. There is no disable of a cron job.
In order to delete a cron job, you should access the Cron Jobs menu and click on standard button as in adding cron jobs above, and then click on delete button which is nearby the job that you no longer need.
Enjoy and happy coding!