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

Estimated reading: 4 minutes 332 views

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. However, cron jobs running on a server will normally use the PHP version set for the server, and not the one specified by the PHP version tool. If your cron job needs to run with a different version of PHP, then you will need to specify it in your 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.

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 for 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 the PHP version on their website, but the upgrade can lead to this error:

Cron mismatch error

In this case, the website uses the newer version of PHP, but the cron jobs use 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
    .deb open with archive manager
  3. This takes you to the cron job setup 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
    .deb open with archive manager
  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.
    .deb open with archive manager

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.

Conclusion

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.

Share this Doc

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

Or copy link

CONTENTS