Laravel Task Scheduling with Cronjob in Shared Hosting C-panel

Hello LaraDevs !

In some of the cases we have to do some task regularly and repeatedly. Some thing like clearing logs every hour, deleting/updating data on a table every day or backing up database twice a day. Manually doing these tasks is quite impossible. so here comes Laravel Scheduler in rescue.

Laravel's command scheduler offers a fresh approach to managing scheduled tasks on your server. The scheduler allows you to fluently and expressively define your command schedule within your Laravel application itself. When using the scheduler, only a single cron entry is needed on your server. Your task schedule is defined in the app/Console/Kernel.php file's schedule method. To help you get started, a simple example is defined within the method.
Source Laravel Docs

In this tutorial I will show you how to achieve task scheduling with cronjob in Laravel Shared hosting c-panel step by step.

Table of content:

1. Define Task Scheduler.

2. Make Cronjob in c-panel

3. Test the Schedule

1. Define Task Scheduler

We define all our task schedules in the App\Console\Kernel class, inside the schedule method. For demo let us take an example of task scheduling . In the example lets schedule a closure to be called every night at 1:00 am.

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // invoke a class to update popular post its just a example.
        // remove this line from your actual file.
        $schedule->call(new updataPopularPost)->dailyAt('1:00');
        
      // for testing
    	$schedule->call(function () {

       	 Log::info('Running');
        
    	})->everyMinute();

    }
} 

here you can define task that you want to perform periodically. That may contain Email sending, generating reports, Updating database tables, executing artisan commands and many more. You can read more in following link

2. Creating Cronjob in c-panel

Lets jump into the c-panel of our domain. Here we will create a cronjob to run our schedules. we will select every minute option.

  • Login to C-panel

  • Navigate to Advanced and select cron jobs.

  • Scroll to Add new Cron Job section and select every minute from the 1st input fields dropdown.

  • add this line in the Command section

     cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 

    make sure to add proper path of your project.

  • Press the Add New Cron Job button

here are some demo images:

Cron-Job tab In C-pannel

Fill out the form

Add New Cron Job Shared Hosting c-panel

3. Test the Schedule

As we have added Log::info('Running'); inside our schedule closure, it will print Running inside our laravel.log file. So lest open our laravel.log file and check.

Here I got Following output:

[2021-03-05 11:15:01] local.INFO: Running
[2021-02-05 11:16:02] local.INFO: Running
[2021-02-05 11:17:01] local.INFO: Running  
[2021-02-05 11:18:01] local.INFO: Running 

Its for only testing purpose, If you get the following result the congratulations. You have successfully able to setup Schedule and Cron job in your Server.

Now you can add actual schedules in the schedule closure.

Finally

In Some shared hosting some functions like shell_exec, exec are disabled for some security reason. If these function do not work make sure it is enabled. If your Schedule is not working make sure your commands are correct and there is no error in your codes. And as Laravel is well documented yo can always read its detailed document in Laravel's official website.

Hope this article helps you to understand the Laravel task schedule and Cron Job set up in your shared Hosted Laravel Application. If you have any questions please let me know in the comment below, Thanks For reading.

Comments :