Skip to main content

Setup Drupal cron through cPanel

Published on
Drupal uses a cron to take care of clean up tasks, as well as utilizing certain features. By default Drupal executes its cron run every three hours based on a user visit. This causes the cron to be executed over HTTP. Depending on the generally it can be more efficient to execute your Drupal cron directly through PHP command line on the server side versus HTTP. This is easily accomplished by setting on a cron job that can be executed at an interval of your choice. Drupal Cron configuration pageFirst get into Drupal and head over to the Configuration -> Cron page (admin/config/system/cron). You're going to want to change "Run cron every" to Never and make note of the URL one this page (best to keep tab/window open.) Think of the cron_key as the cron action's password. Now we need to get the location of your PHP's environment path - this tells the cron job we're executing the file with PHP. Create a phpinfo() file and view it - search (or scroll down to) for the Environment section. The path value should have something similar to usr/bin. Your path to PHP would then be usr/bin/php. cPanel Cron Jobs common settingsWe have our essential bits of information, now log into your host's cPanel and enter the Cron Jobs page. If you're not a Unix pro, don't worry - cPanel has some great common settings built-in that can easily be modified. In the example, I selected "Once an hour" and then adjusted my hour setting to "Every 4 hours." Its up to you how often you want your Drupal cron to run and depends on what kind of services you provide on your website. Okay, now we need to configure our command to be executed. We need to specify the command we're using php from command line, and to execute our Drupal cron.php file, with the cron_key. Here is an example (make sure to remove line break):
cd /home/account_name/public_html/ && 
    /usr/bin/php -f cron.php cron_key=bn3MXFBXo2Z8GCfytxbcmKaZ2_mNrIg_1kGfqQdbRSg
This moves into the public_html directory (change appropriately based on your Drupal installation, replacing 'account_name' with your cPanel login username.) The script then loads cron.php with the -f paramater. This tells PHP to pass parameters to the script - otherwise it would literally look for filename cron.php?cron_key=cd /home/hostjust/public_html/ && /usr/bin/php -f cron.php cron_key=Q27VTve_6LHo74bgAAs2LSrBV3YQdEDi5tzjgKTlJeY not minding the fact there is a GET variable. To test, set the cron job to run every two minutes and set your email address in the field towards the top of the page. If you get an email with PHP notices (since there's no IP_ADDR being sent) you're successfully running your cron! To disable receiving email updates that are just output and not cron errors, add
to the end of your cron command.

I'm available for one-on-one consulting calls – click here to book a meeting with me 🗓️