How to Set Up or Force Cron Jobs to Use a PHP Version

How to Force Cron Jobs to use a PHP version

In all but our oldest servers, we have implemented a feature that allows you to set a version of PHP you want to use on your account. While this works well for files running normally for your site, it will not work for cron jobs. Cron jobs will use the default PHP for the server. In some cases, it will be necessary to specify the use of PHP in the cron job command or script.

The need to force cron jobs to use a PHP version is especially important for some application-specific cron jobs from software like WHMCS. 

The cron PHP version mismatch error is a common error in some applications that will require you to specifically identify the PHP version ‘switch’ for your cron jobs. You will see an example of the PHP Version mismatch error and how to edit the cron job command to use the specific switch leading to the needed PHP version.

Looking for a new WordPress hosting solution? Check out InMotion’s WordPress servers for secure, performance-oriented solutions for your hosting needs.

Common Cron Job Error – Cron PHP Version Mismatch

Typically, this error occurs when there are multiple software being used on a hosting server. The PHP version switcher allows you to specify a newer version of PHP to be used with the website. However, the cron jobs for the software may be using the PHP version configured specifically to the server. This leads to the PHP version mismatch error. You can fix the error by simply forcing the cron job to use a specific PHP version.  

This is a commonly reported error with the WHMCS application.  You may first see a warning about the PHP version that looks something like this:

PHP version warning - WHMCS

This can often lead a person to upgrade their PHP version on their website, but the upgrade can lead to this error:

Cron PHP Version Mismatch error message

In this case, the website is using the newer version of PHP, but the cron jobs are using an older PHP version configured for the server by default. If you see this error, then you will need to set your cron job to use a specific version of PHP. The steps to force cron jobs to use a specific PHP version are described in the next section.

Force Cron Jobs to use a PHP Version

Follow the steps below in order to force your cron job to use a specific PHP version.

  1. First, log into your cPanel dashboard.
  2. Find the Advanced category and click on the Cron Jobs icon.

    cPanel Cron Jobs icon
  3. This takes you to the cron job set up page. At the bottom of the screen you will see the cron jobs you have currently scheduled. Click on the Edit link on the right to edit the cron command.

    cpanel cron job list
  4. You will now see the command line field so you can edit it. The cron command is in two parts, the first part calls the PHP and the second part is the path for the script you want to run.

    edit command option in cron job page


The command code for the sample cronjob is displayed below:

php -q /home/userna5/public_html/cronscript.php

We want to replace the first part of the code (php -q) with (/opt/php74/bin/php -q) for PHP 7.4 and above:

/opt/php74/bin/php -q /home/userna5/public_html/cronscript.php

or (/opt/php72/bin/php -q) for PHP 7.2 as shown:

/opt/php72/bin/php -q /home/userna5/public_html/cronscript.php

Enter the new code at the beginning of the command, then click the Edit Line button to save it. The cron job will now use the correct PHP version. You will need to do this for each cron job so that they are certain to use the correct PHP version.


You should now be able to recognize the common PHP version mismatch error and force cron jobs to use a PHP version that can fix the error.

Improve the performance and security of your WordPress website with our WordPress hosting plans.

check markWeb Application Firewalls    check markFree SSL Certificates    check markAdvanced Server Caching

View WordPress Hosting Plans

AC
Arnel Custodio Content Writer I

As a writer for InMotion Hosting, Arnel has always aimed to share helpful information and provide knowledge that will help solve problems and aid in achieving goals. He's also been active with WordPress local community groups and events since 2004.

More Articles by Arnel

Thoughts on “How to Set Up or Force Cron Jobs to Use a PHP Version

Was this article helpful? Let us know!