A cronjob (short for c(h)ron(ological) job) is like getting the server to automatically perform an action (eg run a php script) on a schedule you specify. It’s straightforward setting up a cronjob using cPanel, all except one step: the command to get the action performed. This article is about setting up a cronjob on a Linux server (part of a LAMP installation) to run a php script.
Step 1: Find out where php is being run from on your server (LAMP server)
This first step is useful for later on and the bit that people new to setting up cronjobs may not be aware of. To enter a command in a cronjob you need to tell the cron handler where php is.
Create where.php and upload to your store’s root directory (eg /public_html):
Then call the script from a browser, eg: www.mysite.com/where.php
Copy the path shown for use in step 5.
(If this doesn’t work, try using usr/bin/php or usr/local/bin/php. If neither of these work, contact your hosting company or tech.)
Step 2: Access cPanel and click the cronjob link:
Step 3: Enter a contact email address
This is so that you can get error messages or completion notifications when the cronjob has run. (However see Step 5 below for a reason why you won’t get any emails.)
Step 4: schedule the cronjob
A dropdown of preset timings should be enough for most people. Point and click. I use the ‘every minute’ schedule to test the cronjob, then change this later to however often it’s needed.
Step 5: THE IMPORTANT ONE – adding in the command
You can break down the command into 3 parts (the [ ] are just there to separate the parts out, but notice the spaces after each part – you need these in the command you write):
[PATH TO PHP] [EXTRA ARGUMENTS] [PATH TO PHP SCRIPT]
This is an example command:
[PATH TO PHP] – you found in Step 1 – here it is /usr/bin/php – this isn’t always the case. Add this at the start of the command. Leave a space after it.
[EXTRA ARGUMENTS] – these are optional, you can use more than one at a time and there are several others. Here I’ve used only -q which is a way of suppressing headers. Leave a space after any arguments.
[PATH TO PHP SCRIPT] – this is the complete path to your php file on your server (not just from the store’s root, the server’s root.)
Added extra (optional)
There is a final piece of code you can add after the [PATH TO PHP SCRIPT]:
Basically this dumps any output from the php script to a ‘blackhole’, so no ‘output’ will be shown on the screen. If you add this to your command it will also stop any email notifications from being sent – as all output is gone, right?
So the image below takes you from Step 4 Scheduling through to now, where you add the command and save the cronjob.
The final step is to add the cronjob, which will then result in this success screen: