How to setup cron job in cPanel hosting?

The cron is a utility service in Linux that is used to execute tasks repetitively. In this tutorial, we will set up a cron job to execute a PHP file per 5 minutes and per minute, per hour, per day, or per month. As you know, the cron job service is used to execute files respectively without any user interaction. You can easily configure the cron job using the cPanel. If you have cPanel hosting and want to create a cron job to execute a file, send automatic reminders, send offer emails per minute, per hour, per day, week, per month then create logic in a file and execute with the command in cron job service.
When you are setting a cron job in the hosting cPanel, the default email would be sent automatically. When the cron job runs, the e-mail account will receive a message. If you are not interested to receive an email notification for a particular job then you can add>/dev/null 2>&1 towards the end of the command. This command would redirect all output to /dev/null. The time interval for the corn jobs can be entered in the next panel in the corresponding boxes.

How to add a cron job in cPanel hosting?

It is a certain procedure to add the cron job in the hosting c panel. You must follow all the steps to add a cron job in the hosting c panel.
1. Find Cron jobs in cPanel –  Login to your cPanel or if you have a hosting provider account, then login to your hosting provider account and find cPanel. Click on cpanel. You will be redirected to the cPanel dashboard where you can set up a cron job for one or more than one file execution. Scroll down and find the advanced section. You can find cron jobs in the advanced section or you can search for cron jobs using the search box in cPanel. Click on the cron jobs option. 

Cron jobs service in cpanel hosting

 2. Configure Cron Job – Setting the cron job is an easy task. You have to understand the below points.
Common setting:
Just use the drop-down list to add your setting in the c-panel. There are different columns you need to add:
Minutes: Add the minute setting for the cron job, to receive a message
Hour: Add the hour setting for the cron job,
Day: Add the day  for the cron job, 
Month: Add the day for the cron job
Weekday: Add the weekday for the cron job 
Command: If you want a special message for cron, just enter it, and you would receive the same message every time.

How to execute a PHP file using the cron job? 

Cron job helps in various ways, such as sending offers per week, alert messages per month, and deleting unused data per minute, per hour, per day, or per month. 
Let's execute a file twice per day. You have to create a command. You need to have a good knowledge of Linux commands before you can use cron jobs effectively but don't worry, It will be super easy using this method. 
Suppose that – You have created a send.php to send emails. You have created this file in the public_html/demo folder 
public_html is a root folder. 
The demo is another folder inside the root folder (public_html)
If you want to execute this file using a cron job, then you have to write a command and set the cron job time interval. 
Select twice per day from command settings. It means the send.php file will execute two times in a day(You can add the hour difference according to your need). You can select different time intervals from the common setting. Like - 
Once Per Minute(* * * * *), Once Per Five Minutes(*/5 * * * *), Twice Per Hour(0,30 * * * *) etc
After that, set up a command. 


/usr/local/bin/php /home/nctest/public_html/demo/send.php

How to configure cron job in cPanel

Here – 
nctest is an account username. Kindly use your same cPanel username. 
public_html - root folder. 
demo – A folder inside the root folder.
We are setting the path for the send.php file. If your file is in the root folder then the command will be like this – 


/usr/local/bin/php /home/nctest/public_html/send.php

You can write the cron job command like this - 


php -q /home/nctest/public_html/demo/send.php

Setup Cron job in hosting cpanel

Set your time interval and then set your command to execute the specific file. 
The cron job assists to schedule your task and duties and saves a lot of your time.

If you want to send email automatically using the PHP code then create a send.php php file.


          $name='Techno Smarter ';
        $msg='This is cron job alert message'; 
       $FromName="Techno Smarter";
$FromEmail=""; // set your website email 
$toemail="xxxxxxxxxxxxxxx"; // set to email 
$subject="New message using cron job"; 
$message='Name:-'.$name.'<br>Email :-' .$email.'<br> Phone No:-'.$phone.'<br> Message:-'.$msg;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: ".$FromName." <".$FromEmail.">\n";
$headers .= "Reply-To: ".$ReplyTo."\n";
$headers .= "X-Sender: <".$FromEmail.">\n";
$headers .= "X-Mailer: PHP\n";
$headers .= "X-Priority: 1\n";
$headers .= "Return-Path: <".$FromEmail.">\n";
   if(mail($toemail, $subject, $message, $headers,'-f'.$FromEmail) ){
         echo '<div class="success"><center><span style="font-size:100px;">&#9989;</span></center> <br> Thank you <strong>'.$name.',</strong> Your message has been sent. We will reply soon as possible. </div> '; 
	} else {
		echo "The server failed to send the message. Please try again later or Make sure , you are using live server for email.";

Note - 1. Kindly set your website email as or in from email variable. 

 2. Set to email - Where you want to receive or send all emails.  Variable toemail 

Benefits of the cron job: 

There are multiple benefits of the cron jobs:
Automated task handling: 
The most obvious benefit is to regulate automated tasks and processes. When you need to control, then can control the minute, the hour, the day, etc
No human intervention:
As the cron jobs are automated, there is no dependence on humans. You can avoid an error as all the processes are controlled by the main server at a specific time.
No occupying any space:
When the cron job is not running, it means the cron is not performing a task. So it not occupying any space on your system RAM when it is not running. You can say, the corn job is not hindering your regular work
Cron Jobs are reliable and predictable:
The Cron Jobs are reliable and predictable, you can implement them without any difficulty to regulate your tasks. You can easily able to regulate your scheme of procedure by implementing the corn jobs.

Examples of corn jobs:

The cron job can be used for various purposes, like monthly reminders, daily feedback from a database, or a monthly reporting system.

Monthly reminders:
The monthly reminders are sent to the users or to a website. This may be filling out a monthly feedback form if the cron job is going to spot any error in filling the feedback form. It is going to send a default message to the clients. 
Daily feed integrations: 
We often need a receive data from a third-party platform to incorporate into our client's websites. For example, getting new stock and importing it to our website.
Monthly reporting System:
Users want to receive a typical PDF format of reports in their inbox rather than logging in to a website and filling out a form to get a result. In this procedure, we add a self-generated report at the start and end of the month. For example the monthly assessment report of employees.

Please Share

Previous Posts:-

Featured Items:-

Payment form using PayUmoney payment Gateway in PHP | Source Scripts


Certificate system in PHP website | PHP scripts


Paypal payment form with email, invoice generator in PHP website | Scripts


0 Comment