This is the most common question we get about CiviCRM. For CiviHosting clients, our answer is very simple:
We will make a cron job for you!
But if prefer to do this yourself (or if we are not your host), we definitely recommend you do and below we will show you two ways how to do it. We recommend using the CLI method, but sometimes it fails to work for reasons not always clear. In such a case, you should try second method, the URL method.
Some of the details below are only relevant for CiviHosting clients — if you use a different host, just adjust those for your host’s Control Panel.
The CLI Method
There are two steps to this method — first we make a .sh file which is the “command” file that the server will execute, and the second step is to create the cron entry to actually execute that file.
Create the .sh File
We recommend putting this file in a private place, for a few reasons. One is so that it won’t get overwritten when you run a CiviCRM upgrade and the second is that we certainly don’t want anyone finding it even, because it has a username and password. To start, login to your hosting Control Panel and click on the File Manager tool. Then at the top of the file list, you will see:
where username is your login. Click on your username there. Now on the right side, click on the link “private”. Now you are in the private area of your account — any files here are not accessible in any way to the public.
The bottom box there is labeled “Create File:” Put in the box there civicron.sh and click the button “Create File”.
Now at the top you will see a button “Edit.” Click on that to edit your new file. In this file, we need to put some code.
For WordPress, use this:
#!/bin/sh php7.cli ~/www/www/wp-content/plugins/civicrm/civicrm/bin/cli.php -u user -p pass -e Job -a execute
It is also possible to use the “cv” CiviCRM CLI Utility. To install it, you would use this code from the CLI:
cd ~/private GET https://download.civicrm.org/cv/cv.phar > cv.phar
and then your cron job would look like this:
#!/bin/sh cd ~/www/www php7.cli ~/private/cv.phar api job.execute --user=admin
For Drupal 7, use this:
#!/bin/sh drush -u 1 -r ~/www/www civicrm-api job.execute auth=0 -y
If you need to run a different version of PHP, such as 7.1, then you would use this:
#!/bin/sh php7.cli /usr/local/bin/drush -u 1 -r ~/www/www civicrm-api job.execute auth=0 -y
Save the file and that’s it. Now the .sh file is ready.
For Drupal 8, add cv.phar as above, and then use this:
#!/bin/sh php7.cli ~/private/cv.phar api job.execute --user=admin --cwd=/home/myuser/www/www/web
You must edit /home/myuser/www/www/web to point to the root of your Drupal 8 install.
Create the Cron Entry
Go to the Cron Jobs section of your hosting Control Panel. In the section on that page called “Schedule a cron job” there is a small icon to the right of the first box. Click on that and a small window will popup. Click on “private” in that window and then on “civicron.sh” and then click the “Select” button.
Now in the box on the main page of the Control Panel you should see “/private/civicron.sh”
In the ‘Choose start time:” selector, you can choose “Every 15 minutes” or “Every 30 minutes” or “Every hour” and then click “Set cron.”
Now to test, click the “Run” button to the right of your new cron job. On the next page, you should see no output. That means it’s working. If it fails, then you may be able to determine the issue from the message — like if it says the username and password aren’t valid, then you must fix those. You can click on the link to the .sh file on the Cron Jobs page to edit.
But if you get another error, one easy trick is to try to switch to the URL method.
Everything here is the same as above, except that the contents of the “/private/civicron.sh” file are a bit different. Here is what you should put there for WordPress:
#!/bin/sh GET 'http://mysite.org/wp-content/plugins/civicrm/civicrm/bin/cron.php?name=admin&pass=MyPass&key=XXX'
and this for Drupal:
#!/bin/sh GET 'http://mysite.org/sites/all/modules/civicrm/bin/cron.php?name=admin&pass=MyPass&key=XXX'
As noted in the comments below, if your site has SSL on it (and that comes free from Let’s Encrypt for all CiviHosting clients) then you would want to use https instead of http in the URL.
Now you must replace user with a CMS user that has full access to CiviCRM and pass with the password of that user. Then you must find your CiviCRM sitekey and replace XXX with the full sitekey.
To find your sitekey, you must first find your civicrm.settings.php file. For WordPress, it’s usually here:
For older sites, it may be in “wp-content/plugins/civicrm/civicrm.settings.php.”
For Drupal here, it should be here:
So you must find that in the File Manager, and then click on it to edit it. A bit more than halfway through the file, you will find a line like this:
define( 'CIVICRM_SITE_KEY', 'cd7238bdkjfiu33kdjkl121kjfiu49b0');
Now you copy that second value, in our case cd7238bdkjfiu33kdjkl121kjfiu49b0 and you replace the XXX with that.
Let us know if you have any questions or comments. 🙂