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.
Checkout more articles on PHP
Steps to set the cron job to run a PHP script in cPanel
- Set a cron job in cPanel
- Edit a cron job
- Set an email for notifications in cron job
- 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.
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.
If you want to set custom time then follow the below cron job format.
- Minute (0 to 59)
- Hour ( 0 to 23)
- Day of month (1 to 31)
- Month (1 to 12)
- Day of week (0 to 6 are Sunday to Saturday)
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
1 | php -q /home/cpanelUser/public_html/script.php |
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.
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.
Edit the settings that you want to make and click on the Edit Line button.
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.
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.
1 | php -q /home/cpanelUser/public_html/script.php >/dev/null 2>&1 |
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.
1 2 3 4 | CREATE TABLE `cron` ( `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `created_date` DATETIME NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!--?php // Database Connection $dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $db = "demo"; $con = mysqli_connect($dbhost, $dbuser, $dbpass , $db) or die($link); // insert data in to database $currentTime = date('Y-m-d H:i:s'); $sql = "INSERT INTO `cron` (created_date) VALUES ('$currentTime')"; $insertData = mysqli_query($con,$sql); ?--> |
Note: Cron jobs are run based on the server time.
Thank you for reading. Happy Coding!