0% found this document useful (0 votes)
125 views8 pages

Cron Job PHP

The document explains how to set up and configure cron jobs to automatically run PHP scripts on a schedule in cPanel. It covers adding a new cron job, editing settings, setting an email notification, and provides an example PHP script to insert the current date and time into a database table when run by the cron job.

Uploaded by

sodat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views8 pages

Cron Job PHP

The document explains how to set up and configure cron jobs to automatically run PHP scripts on a schedule in cPanel. It covers adding a new cron job, editing settings, setting an email notification, and provides an example PHP script to insert the current date and time into a database table when run by the cron job.

Uploaded by

sodat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Skip to content

ReactJS

JavaScript

Node.js

Next.js

PHP

Git

React Testing Library

Interview Questions

PHP

13,517

Set the cron job to run a PHP script in cPanel

BY CLUE MEDIATOR · MAY 6, 2020

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.

Checkout more articles on PHP

Partially hide email address in PHP

Ajax POST request with JQuery and PHP

Convert base64 to image file in PHP

Autocomplete Textbox Using PHP, MySQL and jQuery

How to use session in 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.

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

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

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

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

Edit the settings that you want to make and click on the Edit Line button.

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

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.

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

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!

If you found value in this article,

you can support us by buying me a coffee! ☕

Like Follow Follow Subscribe Join Follow

Tags: Setup

YOU MAY ALSO LIKE...

Find URLs in a string and make clickable links in PHP

DECEMBER 29, 2020

11,330

How to fix phpMyAdmin error – Incorrect format parameter

OCTOBER 30, 2020

9,931

Convert XML to JSON in PHP

DECEMBER 4, 2020

1,236

Add or remove input fields dynamically in PHP using jQuery

JUNE 3, 2020

16,482

Add or Subtract days, month and year to a date using PHP

JANUARY 29, 2020

27,739
Get current page URL in PHP

FEBRUARY 19, 2020

9,494

LEAVE A REPLY

Comment

Name *

Email *

Yes! Add me to your newsletter!

FOLLOW:

JOIN OUR NEWSLETTER

Subscribe to our free, once-weekly email filled with coding news & articles.

RECENT POSTS

How to sort a date string array with JavaScriptMay 28, 2022

Divide an array in half using JavaScriptMay 23, 2022

How to get the current day, month, and year in JavaScriptMay 18, 2022

How to create a GIF from images in ReactMay 15, 2022


React code editor and syntax highlighter using PrismJSMay 11, 2022

TAGS

AG Grid Ajax API Array Base64 Canvas Conditional Rendering Cookie CORS DateTime
Dropdown Email Error File or Directory File Upload Form Google Maps Image JavaScript
for React jQuery JWT Location Login App MySQL Number Pagination Password Picker
Popup react-select React Advanced React Beginner React Example React Hooks React
Hooks Beginner React Package Redirection Regular Expression Routing Scroll Setup
String Table URL Validation

JOIN US

Like us on FacebookFollow us on TwitterFollow us on GitHubSubscribe us on


YouTubeJoin us on TelegramFollow us on Instagram

TOP POSTS

Login App – Create login form in ReactJS using secure REST API – Part 3

Add or remove input fields dynamically with ReactJS

Navigate from one page to another page in ReactJS

Ajax POST request with jQuery and PHP

Create simple Popup in ReactJS

How to get selected by only value in react-select

EXPLORE THE ARTICLE

REACTJS

Attempted import error: ‘Switch’ is not exported from ‘react-router-dom’

REACTJS

Add space between carousel items in react-slick

JAVASCRIPT

How to set an HTML element’s class using JavaScript

JAVASCRIPT

7 JavaScript One-Liners that you should know

PHP

Get location from an IP address in PHP


We are not simply proficient at writing blog post, we’re excellent at explaining the way
of learning which response to developers.

For any inquiries, contact us at [email protected].

QUICK LINKS

About

Contact Us

Privacy Policy

Clue Mediator © 2022. All Rights Reserved.

You might also like