How to set up Cron Jobs with Drupal 7
We were recently asked by a customer how to set up a cron job with Drupal7. To assist with this and to help other customers in the future, we have created this Drupal 7 Cron Guide.
This guide will likely work with other hosts so if you are having issues getting cron to work, the below may help.
Step1 : Find your cron job in Drupal 7
Open up your Drupal admin, click "configuration" then click "cron" from the "system" menu.
You should now see a popup window with your drupal cron job url.
The key should say something like:
"To run cron from outside the site, go to http://your-domain.co.uk/cron.php?cron_key=AGBTjC54tml2GGKTwsYJWvR35Q1DBF805nUHyc9ibEo" where the "your-domain.co.uk" is your domain name.
Copy this as you will need it later.
Next, you will need to load up your eXtend control panel so we can access the scheduled tasks section (if you are using another host, ask them where you can manage/setup cron jobs).
In your extend control panel, scroll down to the "Web Tools" section then click "scheduled tasks".
Scroll down to the "Configure Scheduled Tasks". You should see your full site's path in bold.
What you will need to do is work out the full path of your drupal cron file. On our servers, the path will be /home/sites/your-domain.co.uk/public_html/ where "your-domain.co.uk" is replaced with your own domain.
By default, all website files will be inside the public_html folder. So if you type in http://your-domain.co.uk into the browser and your website loads up, your website's files are in the /home/sites/your-domain.co.uk/public_html/. If you have to type in http://your-domain.co.uk/website/ to load your website, your site is located in /home/sites/your-domain.co.uk/public_html/website/
So if we look at the cron job copied from earlier:
we can see that the cron file is located in the public_html (since there is no mention of a folder after your-domain.co.uk/
As such, the full path to the cron file would be:
If you are not using Squirrel Hosting, you will need to contact your host for the full path.
Now we know the full path to the cron file. The next step is to break the cron job down into a full command so it can be run. This would be created by using the following
/[INTERPRETER] /[FILE] [ARGUMENT]
What does this mean?
[INTERPRETER] is the path to the right version of the language. If the file is a php file (which cron.php is), you would want to run it in php. As such, you would use /usr/bin/php56 for php version 5.6 (if you wanted to use e.g. php version 7, you would use /usr/bin/php7).
[FILE] is the file in question e.g. cron.php.
[ARGUMENT] is the argument for the script e.g. cron_key=AGBTjC54tml2GGKTwsYJWvR35Q1DBF805nUHyc9ibEo.
So using all the above, the cron job which Drupal provided:
would be converted into:
/usr/bin/php56 /home/sites/your-domain.co.uk/public_html/cron.php cron_key=AGBTjC54tml2GGKTwsYJWvR35Q1DBF805nUHyc9ibEo
Important : note the space after the [INTERPRETER] (between the 56 and /home) and the ? has been removed (between the cron.php and cron_key).
Now you know your cron job command, you can enter it into the scheduled tasks page.
Under the "Configure Scheduled Tasks", enter your drupal cron job which you have converted above into the "Run this command" box.
You can test the cron is correct by clicking the "Test Command" button.
Next, you will need to specify how often you would like the cron job to run using the drop down boxes below.
Next, click the "update" button at the bottom of the page to set the cron job active.
Once the cron job has run (or if you click the "test command" button), you should see it in your Drupal admin.
If you need any help with the above and you have hosting with us, please open a support ticket and we will do our best to help.
Leave a comment
Has this guide helped you? Have an idea for a guide or need help? Let us know below.