Set the cron job to run a PHP script in cPanel

Today we will explain how to set cron jobs and run php scripts using cron jobs. Let you explain what is cron before we start.

What is a cron job?

A cron job is a Linux command which schedules a script on your server to run automatically at defined times or regular intervals. Cron is used to ensure timely execution of scripts and also used for deleting temporary files to conserve your disk space, for backups.

How to run PHP scripts from cron jobs, Execute PHP script in cron job , how to run php file using cron jobs, How to run a PHP script using a Cron job, Run a PHP file in a cron job using CPanel, how to run a php script daily with the cron job, How to set the cron job to run a PHP script in cPanel, Run cron job on cpanel with php7 version, Cron Job, Run PHP script, php cron scheduler, on linux centos how set cron job for php file.

Steps to set the cron job to run a PHP script in cPanel

  1. Set a cron job in cPanel
  2. Edit a cron job
  3. Set an email for notifications in cron job
  4. PHP Script which run via cron jobs

1. Set a cron job in cPanel

The following steps are used to set the cron job in cPanel.

Step 1: Login to cPanel

Login to cPanel and go to ADVANCED > Cron Jobs.

Cron job - Clue Mediator
Cron job – Clue Mediator

Step 2: Cron job settings

In the Add New Cron Job section, you can select common settings or add custom values. Here we set a cron job twice per hour.

Cron job settings - Clue Mediator
Cron job settings – Clue Mediator

Step 3: Add command to set a cron job

In the Command field, if you want to run PHP Script file named script.php that located in the public_html directory then command will be

Cron job command - Clue Mediator
Cron job command – Clue Mediator

After adding the command in the Command textbox, click on the Add New Cron Job button to set your cron job and it will appears in the list as shown below.

Cron job list - Clue Mediator
Cron job list – Clue Mediator

2. Edit a cron job

To edit a cron job, follow as below.

  • Click on the Edit button that is located in the cron job list to edit the cron job.

    Cron job edit action - Clue Mediator
    Cron job edit action – Clue Mediator
  • Edit the settings that you want to make and click on the Edit Line button.

    Edit cron job - Clue Mediator
    Edit cron job – Clue Mediator

3. Set an email for notifications in cron job

Also we can notify via email when a cron job runs, just we need to add an email address in the Cron Email section.

Cron job email notification - Clue Mediator
Cron job email notification – Clue Mediator

If more than one cron job is set and you do not want email notifications for a particular cron job then you can add >/dev/null 2>&1 after command like below.

4. PHP script that execute via cron jobs

Now we will write PHP Script to add the current date and time in the database and it will automatically run as per the cron job interval.

Create a table

Use the below code to create a table cron in the database.

Insert data into the database

Now, we create a php file named script.php and write a PHP script to insert the current date and time into the datatable. When the cron job runs at that time the current date and time will be inserted into the datatable. So that means the cron job is running according to the interval.

script.php

Note: Cron jobs are run based on the server time.

Thank you for reading. Happy Coding!

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *