Introduction
Cron jobs allow you to execute tasks automatically at a scheduled time without manually running them. In CodeIgniter, cron jobs can be used for tasks such as sending daily emails, generating reports, database cleanup, and other scheduled operations.
In this article, we will explain how to run a CodeIgniter function as a cron job using cPanel.
Prerequisites
Before proceeding, make sure you have:
- A working CodeIgniter application.
- Access to the cPanel account.
- Access to the CodeIgniter application files.
- A valid email configuration if the cron job sends emails.
- Permission to create cron jobs in cPanel.
Implementation
Step 1: Create a Cron Job in cPanel
Log in to cPanel and navigate to:
cPanel → Cron Jobs
Select the required execution interval and add the following command:
wget -q -O - http://domainname.com/Cronjob/sendDailyEmail
Replace domainname.com with your actual domain name.
The URL points to the CodeIgniter controller and method that needs to be executed by the cron job.
Step 2: Create the CodeIgniter Controller
Create a controller named Cronjob.php inside the CodeIgniter controllers directory.
Path:
/application/controllers/Cronjob.php
Add the following code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Cronjob extends MY_Controller
{
public function sendDailyEmail()
{
$userEmail = 'example@domain.com';
$subject = 'Test';
$config = array(
'mailtype' => 'html',
'charset' => 'utf-8',
'priority' => '1'
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('example@domain.in', 'Test');
$data = array(
'userName' => 'Test'
);
$this->email->to($userEmail);
$this->email->subject($subject);
$body = $this->load->view('birthday.php', $data, TRUE);
$this->email->message($body);
$this->email->send();
}
}
In the above code, the sendDailyEmail() function contains the email-sending logic.
The cron job will access the following URL:
http://domainname.com/Cronjob/sendDailyEmail
Step 3: Create the Email Template
Create an HTML file named birthday.php inside the CodeIgniter views directory.
Path:
/application/views/birthday.php
Add the following HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CodeIgniter Mail Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<p style="margin-top: 0; color: #565656; font-family: Georgia, serif;
font-size: 16px; line-height: 25px; margin-bottom: 25px;">
Hey <?php echo $userName; ?>,
</p>
<p style="margin-top: 0; color: #565656; font-family: Georgia, serif;
font-size: 16px; line-height: 25px; margin-bottom: 25px;">
This is a demo email sent using an HTML template in CodeIgniter.
</p>
</body>
</html>
You can also add your company logo and other HTML elements to the email template as required.
Step 4: Test the Cron Job
Before relying on the scheduled cron job, test the CodeIgniter URL manually:
http://domainname.com/Cronjob/sendDailyEmail
If the configuration is correct, the sendDailyEmail() method will execute and send the email.
After confirming that it works, the cPanel cron job will execute the URL automatically according to the configured schedule.
Note: Using
wgetto execute a web URL works, but for production environments it is generally better to run scheduled application tasks through the PHP CLI when the CodeIgniter application and hosting environment support it. Also, protect cron endpoints from unauthorized public access where appropriate.
Conclusion
In this article, we explained how to run a CodeIgniter function as a cron job using cPanel. We created a CodeIgniter controller, added the required function, created an HTML email template, and configured a cPanel cron job to execute the function automatically.
Cron jobs can be useful for automating repetitive tasks such as email notifications, report generation, database maintenance, and scheduled application processes.
FAQs
1. What is a cron job?
A cron job is a scheduled task that runs automatically at a specified time or interval.
2. Can I run a CodeIgniter function using a cPanel cron job?
Yes. You can configure a cPanel cron job to access a CodeIgniter controller method through a URL or execute the application using PHP CLI.
3. How do I access the CodeIgniter cron function?
For the example above, the function can be accessed using:
http://domainname.com/Cronjob/sendDailyEmail
4. Can I use cron jobs to send daily emails?
Yes. A cron job can execute a CodeIgniter function at a scheduled time, which can then send daily or periodic emails.
Related Article
How to add Google ReCAPTCHA in PHP Codeigniter – Pheonix Solutions
UPLOAD FILE USING CODEIGNITER FRAMEWORK – Pheonix Solutions
Talk to our experts
Have a technology challenge or looking for the right solution for your business? Our team can help you with cloud, DevOps, development, infrastructure, design, and more. Feel free to reach out to our experts here.