How To Setup Cron Job Task Scheduler In Laravel 10

Websolutionstuff | Apr-05-2023 | Categories : Laravel

In this article, we will delve into the process of setting up a cron job task scheduler in Laravel 10. Our focus will be on understanding and implementing the Laravel 10 cron job task scheduler. Many scenarios require running specific code at regular intervals in Laravel 10, and manually executing these tasks each time can be cumbersome.

However, by utilizing the Laravel scheduler, we can automate and create cron jobs efficiently.

Throughout this article, we will cover various aspects of Laravel 10's cron job task scheduling, including creating and managing cron jobs. By the end, you will be well-equipped to automate scheduled tasks in your Laravel 10 application, enhancing its efficiency and reliability.

Step 1: Install Laravel 10

In this article, we will install laravel 10 using the following command.

composer create-project laravel/laravel laravel-10-cron-job-example

 

 

Step 2: Create New Command

Now, we will create a new cron command using the following command.

php artisan make:command TestCron --command=test:cron

app/Console/Commands/TestCron.php

<?php
  
namespace App\Console\Commands;
  
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use App\Models\User;
  
class TestCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:cron';
  
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';
  
    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        info("Cron job running at ". now());
    
		/*
			Write your database logic we bellow:
			User::create(['email'=>'send mail']);
		*/

    }
}

 

Step 3: Register as Task Scheduler

In this step, we will define our commands on the Kernel.php file.

We've already seen a few examples of how you may configure a task to run at specified intervals. However, there are many more task schedule frequencies that you may assign to a task.+

Method Description
->cron('* * * * *'); Run the task on a custom cron schedule
->everyMinute(); Run the task every minute
->everyTwoMinutes(); Run the task every two minutes
->everyThreeMinutes(); Run the task every three minutes
->everyFourMinutes(); Run the task every four minutes
->everyFiveMinutes(); Run the task every five minutes
->everyTenMinutes(); Run the task every ten minutes
->everyFifteenMinutes(); Run the task every fifteen minutes
->everyThirtyMinutes(); Run the task every thirty minutes
->hourly(); Run the task every hour
->hourlyAt(17); Run the task every hour at 17 minutes past the hour
->everyOddHour(); Run the task every odd hour
->everyTwoHours(); Run the task every two hours
->everyThreeHours(); Run the task every three hours
->everyFourHours(); Run the task every four hours
->everySixHours(); Run the task every six hours
->daily(); Run the task every day at midnight
->dailyAt('13:00'); Run the task every day at 13:00
->twiceDaily(1, 13); Run the task daily at 1:00 & 13:00
->twiceDailyAt(1, 13, 15); Run the task daily at 1:15 & 13:15
->weekly(); Run the task every Sunday at 00:00
->weeklyOn(1, '8:00'); Run the task every week on Monday at 8:00
->monthly(); Run the task on the first day of every month at 00:00
->monthlyOn(4, '15:00'); Run the task every month on the 4th at 15:00
->twiceMonthly(1, 16, '13:00'); Run the task monthly on the 1st and 16th at 13:00
->lastDayOfMonth('15:00'); Run the task on the last day of the month at 15:00
->quarterly(); Run the task on the first day of every quarter at 00:00
->quarterlyOn(4, '14:00'); Run the task every quarter on the 4th at 14:00
->yearly(); Run the task on the first day of every year at 00:00
->yearlyOn(6, 1, '17:00'); Run the task every year on June 1st at 17:00
->timezone('America/New_York'); Set the timezone for the task

 

app/Console/Kernel.php

<?php
  
namespace App\Console;
  
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
  
class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
        $schedule->command('test:cron')->hourly();
    }
  
    /**
     * Register the commands for the application.
     */
    protected function commands(): void
    {
        $this->load(__DIR__.'/Commands');
  
        require base_path('routes/console.php');
    }
}

 

 

Step 4: Run Scheduler Command For Test

Now, we will run the cron job using the following command.

php artisan schedule:run

storage/logs/laravel.php

[2023-03-14 04:00:02] local.INFO: Cron job running at 2023-03-14 04:00:02  
[2023-03-14 05:00:01] local.INFO: Cron job running at 2023-03-14 05:00:01  
[2023-03-14 06:00:02] local.INFO: Cron job running at 2023-03-14 06:00:02  

 

Cron Job Setup on Server

Now, we will set up a cron job on the server. You need to install crontab on the server. if you are using an ubuntu server then it is already installed. So, run the following command.

crontab -e

Now, add the below line to the crontab file. make sure you need to set your project path correctly on it.

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

 

Conclusion:

In conclusion, we've explored the essential process of setting up a cron job task scheduler in Laravel 10. By harnessing the power of Laravel's built-in scheduler, we've learned how to automate and manage scheduled tasks effectively.

Automating tasks at specific intervals is a common requirement in web development, and Laravel's task scheduler simplifies this process. Whether you need to run routine maintenance, generate reports, or perform any other recurring tasks, the Laravel 10 cron job task scheduler provides a convenient solution.

 


You might also like:

Recommended Post
Featured Post
How to Install PHP DOM Extension in Ubuntu 23.04
How to Install PHP DOM Extensi...

In this tutorial, I will guide you through the process of installing the PHP DOM Extension in Ubuntu 23.04. The PHP DOM...

Read More

Jan-29-2024

Angular 15: Unleashing Modern Web Development
Angular 15: Unleashing Modern...

Angular 15 is the latest iteration of one of the most popular JavaScript frameworks for building dynamic web application...

Read More

Jun-05-2023

How to Install Material Theme in Angular 17
How to Install Material Theme...

In this article, we'll install the material theme in angular 17. Material Theme brings a polished design and us...

Read More

Mar-29-2024

How To Generate RSS Feed In Laravel 9
How To Generate RSS Feed In La...

In this article, we will see how to generate an RSS feed in laravel 9. Here, we will generate an RSS feed in larave...

Read More

Dec-12-2022