1. Linux

How to create a cronjob to run a PHP script (cPanel)

In this article we will learn how to create a cronjob to run a PHP script in cPanel. We have recently updated cPanel to the Jupiter theme, the latest one, so do not be worried if our interface looks different from your cPanel theme.

Login to cPanel

Just in case you do not know where cPanel is located, you can get there in two ways.

  • cPanel is generally located on port 2083, so you can access the login with the IP and port, for example: https://192.168.0.1:2083.
  • Another option is to access cPanel from the WHM server administration panel, like it’s show in the image below.
login to cpanel from WHM
Access cPanel from WHM

Creating a cron to run PHP

Once you are in cPanel, navigate to the Advanced section and find Cron Jobs icon.

location of cron jobs in cPanel
cPanel Advanced > Cron Jobs

Cron job is a handy tool which allows to automate certain tasks on the server. For example, your site may need to run certain script once a day to create a backup or send notifications to customers etc. With the cron job you can run a certain script in defined times with a set frequency.

set email for cron job results
Cron email notifications in cPanel

If you want an email to be sent every time cron job is run, you can set one in the Cron Email like on the image above. But generally it’s not very useful, because most times you will know by itself if the script runs ok or something needs to be fixed.

add cron job cpanel
Cron Job settings in cPanel

cPanel now has a nice intuitive interface for cron jobs creation. You can set your own intervals for the cron job or you can select one of the most commonly used within Common Settings.

Twice per day cron job

Here we have chosen the Twice Per Day in the common settings and all other fields were filled automatically.

Now we only need to specify the command and add the cron job. For this task we will need two things:

  • Location of the script: we will need full path on the server. On cPanel it generally looks like, /home/username/public_html/location/of/the/file.php
  • Command to run a PHP script. For cPanel, it will be: /usr/local/bin/ea-php74. You need to replace ea-php75 with your PHP version on the server, it can be ea-php76 or ea-php80 or some other.
creating a cron job to run php script once per week
Creating a cron job

We only need to press “Add New Cron Job” and it will appear in the list of “Current Cron Jobs“.

Current Cron Jobs (cPanel)

This is it on how to create a cron job and schedule to run a PHP script with its help. You can edit or remove newly created crons any time.