How To Setup Cron Job Task Scheduler In Laravel 10

    In this article, we will see how to set up a cron job task scheduler in laravel 10. Here, we will learn about the laravel 10 cron job task scheduler. Many times we need to require to run some piece of code in a specific interval time period in laravel 10 and we need to run it manually every time but using the scheduler through we can run and create a cron job in laravel 10.

    So, let's see laravel 10 cron job task scheduling, cron job in laravel 10, schedule cron job in laravel 10, laravel 10 cron job schedule, cron job scheduling in laravel 10, how to create a cron job in laravel 10, how to run cron job in laravel 10.

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
Read Also: How To Create User Roles And Permissions In Laravel 10
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');
    }
}
Read Also: How to Set Auto Database BackUp using Cron Scheduler In Laravel
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

    You might also like:

Bình luận
Vui lòng đăng nhập để bình luận
Một số bài viết liên quan