Setting up cron using cPanel
Cron is a time-based scheduler that is used extensively throughout CiviCRM. For example, you might want to use CiviCRM to send out an e-mail newsletter at a particular time, or you might want to send out a reminder to participants to attend an event. CiviCRM has settings to accomplish all these tasks, but these, in turn, rely on having "master" cron set up. Cron is set up on your web server, not within CiviCRM.
How to do it…
There are many different ways of setting up cron, depending on your site-hosting setup. In this example, we are using cPanel, a popular control panel that simplifies website administration.
- Make a note of your CMS site administrator username and password.
- Make a note of your CiviCRM site key, which is a long string of characters used to uniquely identify your CiviCRM installation. It is automatically generated when CiviCRM is installed, and is stored in the
civicrm_settings.php
file. Using a text editor, open up the CiviCRM settings file located at/sites/default/civicrm_settings.php
. Around line 170, you will see the following entry:define( 'CIVICRM_SITE_KEY', '7409e83819379dc5646783f34f9753d9' );
Make a note of this key.
- Log in to cPanel and use the cPanel File Manager to explore the folders and files that are stored there. You are going to create a file that contains all the necessary information for cron to work. You can choose to create the cron file anywhere you like. It makes sense to keep it in the home directory of your webserver—that is, the first directory you get to once you start exploring.
- Create a file called
CiviCron.php
. The naming does not particularly matter, but it must be a PHP file. - Insert the following code:
<?php // create a new cURL resource $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, "http://myDrupalsite.com/sites/all/modules/civicrm/bin/cron.php?name=admin&pass=adminpassword&key=01504c43af550a317f3c6495c2442ab7"); curl_setopt($ch, CURLOPT_HEADER, 0); // grab URL and pass it to the browser curl_exec($ch); curl_close($ch); ?>
- Substitute
http://myDrupalsite.com
with your own domain - Substitute
admin
with your own CMS admin username - Substitute
adminpassword
with your own CMS admin password - Substitute the key value with the site key from
civicrm_settings.php
- Substitute
- Save this file and then navigate to
cron
in cPanel. - Select an appropriate cron interval from the Common Settings list. Choosing an appropriate cron interval may take some experimentation, depending on how your site is set up. In the Command field, enter the following address:
php /home/site_account_name/public_html/CiviCron.php
The portion after
php
is the absolute path to theCiviCron.php
file you created in step 4. - Click on Add New Cron Job.
How it works…
All cron does is execute the URL that is constructed in the cron file.
The following piece of code does the work:
curl_setopt($ch, CURLOPT_URL, "http://myDrupalsite.com/sites/all/modules/civicrm/bin/cron.php?name=admin&pass=adminpassword&key=01504c43af550a317f3c6495c2442ab7");
The URL contains the information on permissions (the username, the password, and the site key) to execute the cron.php
file provided by the CiviCRM module.
Getting cron to work is critical to getting CiviCRM working properly. If you get into difficulties with it, the best solution is to contact your hosting company and seek guidance.
Tip
To test that your cron job is actually working, carry out the following instructions. In the cPanel cron screen, set it to send you an e-mail each time the cron command is run. The e-mail will contain an error message if the cron fails. Failures are generally due to an incorrect setting of the path, or a permissions problem with the username, password, or site key.