Setup WHMCS Cron Jobs on Ubuntu

You are here:
← All Topics

We need to verify that the cron package is installed.

dpkg -l cron

Output should be similar to this example if the cron package is installed:

||/ Name Version Architecture Description
+++-=========================-=================-=================-========================================================
ii cron 3.0pl1-128ubuntu2 amd64 process scheduling daemon

 

We can install cron package if necessary.

sudo apt-get install cron

 

Ensure that the cron service is running with the following command:

systemctl status cron

 

Example response:

cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2019-08-24 15:57:14 UTC; 1 months 18 days ago
Docs: man:cron(8)
Main PID: 762 (cron)
Tasks: 1 (limit: 1146)
Memory: 38.6M
CGroup: /system.slice/cron.service
└─762 /usr/sbin/cron -f

 

We need to configure the cron job.

When you are logged in as your user, you are creating a cron job under that user. Creating a cron jobs owner is helpful when to know who is in charge of the cron as well as how to alter the cron job in the future.

crontab -e

The vim editor will sometimes open by default when calling crontab -e. I prefer to use nano, to launch crontab with the nano editor use the following command:

EDITOR=nano crontab -e

In this file, you’ll see a bunch of # commented lines explaining how to use the file. Move to the bottom of the file and copy your cron command from from the WHMCS admin area. Setup -> Automation Settings. It should look something like this example.

*/5 * * * * /usr/bin/php -q /var/www/exampledomain.com/public_html/accounts/crons/cron.php
*/5 * * * * /usr/bin/php -q /var/www/exampledomain.com/public_html/accounts/crons/cron.php

At the beginning of the cron command you see 5 asterisk ***** each * designates the time for that section. Position 1 represents minutes, position2 hours, position 3 day of the month, etc.

The asterisks represent all values for that set. Placing an asterisk on the month column will tell the system to perform the tasks every month.

 

Our cron command for WHMC is set to run every 5 minutes, on every day of the week, every month, etc:

*/5 * * * * /usr/bin/php -q /var/www/exampledomain.com/public_html/accounts/crons/cron.php

 

After the time digits section is the php file that should be executed to run the WHMCS cron

/usr/bin/php -q<

Finally the last section assigns the command to run, for WHMCS that is the cron.php file which is typcially located in /accounts/crons/cron.php

 

Your crontab file should now look something like this:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
*/5 * * * * /usr/bin/php -q /var/www/exampledomain.com/public_html/accounts/crons/cron.php

When you are done, save the file Ctrl+O, Exit nano Ctrl+X

 

You can do a test run of the WHMCS cron job by entering the following command:

/usr/bin/php -q /var/www/exampledomain.com/public_html/accounts/crons/cron.php