Cron Job PHP
Cron Job PHP
ReactJS
JavaScript
Node.js
Next.js
PHP
Git
Interview Questions
PHP
13,517
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.
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.
The following steps are used to set the cron job in cPanel.
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)
Month (1 to 12)
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.
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.
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.
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
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";
$insertData = mysqli_query($con,$sql);
?-->
Note: Cron jobs are run based on the server time.
Tags: Setup
11,330
9,931
DECEMBER 4, 2020
1,236
JUNE 3, 2020
16,482
27,739
Get current page URL in PHP
9,494
LEAVE A REPLY
Comment
Name *
Email *
FOLLOW:
Subscribe to our free, once-weekly email filled with coding news & articles.
RECENT POSTS
How to get the current day, month, and year in JavaScriptMay 18, 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
TOP POSTS
Login App – Create login form in ReactJS using secure REST API – Part 3
REACTJS
REACTJS
JAVASCRIPT
JAVASCRIPT
PHP
QUICK LINKS
About
Contact Us
Privacy Policy