Table of contents

laravel schedule

In this post, you will learn how to implement Laravel 9 cron job task scheduling. I will give you a basic example so that it will be easier for you to understand. A cron job is an automation that will help us to let the system work for the task like sending promotional emails, SMS blasting, generating weekly reports, creating monthly backups, sending subscription expiration with a specified time, and many more.

 

Laravel offers an awesome task scheduling mechanism that easier for us to do it. To learn about this just follow my simple steps and implement it in your Laravel application.

 

Step 1: Laravel 9 Installation


In this step, let's install our Laravel 9 so that we can implement our task scheduling. Just run the following command below to do install:

 

composer create-project --prefer-dist laravel/laravel task-scheduling

cd task-scheduling

 

Step 2: Create a Command for Task Scheduler

In this step, we will create our custom command to execute our task scheduling via cron job. To do this just run the following command below:

 

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

 

Now it will create a file inside app/Console/Commands/TestCron.php. Now let's view it and add the whole code below:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

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 = 'Testing cron command.';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info("Testing with cron job.");
    }
}

 

Step 3: Setup Task Scheduler

 

In this section, we will set up our task scheduler with our command created above and set the schedule when it will execute. To do that we should define it inside the app/Console/Kernel.php. Before that, you should learn first lists of available schedule methods.

 

->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
->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
->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
->monthlyOnLastDay(’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
->yearly(); Run the task on the first day of every year at 00:00
->timezone(‘America/New_York’); Set the timezone

 

For more about the available methods kindly visit the official documentation.

 

Now let's define our task schedule inside app/Console/Kernel.php file. See the working source code below:

 

<?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.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('test:cron')
            ->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

 

As you can see above we added the line $schedule->command('test:cron')->everyMinute(); inside the schedule method.

 

Step 4: Testing the Task Scheduler Command

Since we already set up our scheduler we will test it with the following command below if it is working correctly.

 

php artisan schedule:run

 

Then this will be the output after we execute the command above.

 

[2022-05-30T14:17:04+00:00] Running scheduled command: "C:\php8\php\php.exe" "artisan" test:cron > "NUL" 2>&1

 

And this is the output inside our storage/logs/laravel.log file.

 

[2022-05-30 14:17:04] local.INFO: Testing with cron job.

 

In this case, since our Laravel task scheduler is working we will setup now the corn job.

 

In you server SSH execute the following command:

crontab -e

 

Then input the following command below:

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

 

That's pretty much it. I hope it helps. Thank you for visiting.