Stablepoint Knowledge Base

Get all the help you need here.

Cron Jobs

Posted 07th August, 2019

Cron is a utility, implemented in Linux, which schedules a command or script on your server to run automatically at a specified time and date.

A cron job is the scheduled task itself. It can be set in the cPanel control panel in multiple ways:

  1. As a php command on the server using /usr/bin/php - this is required if your script requires PHP to be executed.
  2. As a wget command, which essentially fetches the web page. This might be used, for instance, to call a script that you could run in the browser yourself.
  3. You could use a curl request.

Which method you use depends on what your cron job should do once executed. If you are using third party software, follow their guidance.

Creating your cron job

Cron jobs can either be created in the cPanel interface, or through the server command line using SSH.

If creating through the cPanel interface (recommended) you can log into cPanel, and then click Cron Jobs. You will see a list of current crons and the ability to add a new one.

If using the command line you can run crontab -e which will bring up the file you can edit. Saving the file will automatically update your cron schedule.

Cron scheduling

Cron jobs are created on a schedule. You can define how frequently you would like your cron to run - anything from yearly, to constantly.

cPanel includes an interface for defining your cron job schedule and adding the task. You can simply choose Once a month for instance.

If creating a manual schedule, please follow the below guidelines. The crontab format is somewhat arcane and cryptic, so we will take this step by step.

The basic format of a crontab schedule consists of 6 fields, placed on a single line and separated by spaces, in the below order:

  • minute (between 0 and 59)
  • hour (between 0 and 23)
  • day (between 1 and 31)
  • month (between 1 and 12)
  • day-of-week (between 0 and 7 where both 0 and 7 denote Sunday)
  • command-line-to-execute (the script to run)

The fields have to be in that exact order, with no empty or missing fields, and everything must be placed on a single line.

To run every time - i.e. every minutes - you replace the integer with an asterix (*).

Schedule Examples

If you want a command run on 5th January at 9.15 am, your schedule should begin with the following:

15 9 5 1

If you wish to run a particular program once a day at 10:45am

> 45 10 * * *

Commands

After the scedule is defined, the path to the script can then be added.

If you have a cron file in your website home folder (yourdomain.com/cron.php), then you could call it in one of two ways.

First, using php from the server:

45 10 * * * /home/username/public_html/cron.php

Second, as a wget command:

45 10 * * * /usr/bin/wget https://yourdomain.com/cron.php

Thirdly, as a curl command:

45 10 * * * /usr/bin/curl https://yourdomain.com/cron.php

Outputting Cron Logs

You can output the logs from the cron by adding a > sign at the end with the path to the log. For example:

45 10 * * * /home/username/public_html/cron.php > /home/username/public_html/cron.log