How to scheduling jobs in Laravel 9

Cron job schedule task in Laravel 9

Posted by Margie Crona on November 26, 2022

How to scheduling jobs in Laravel 9

Cron job schedule task in Laravel 9

Posted by Margie Crona on November 26, 2022

Sometimes you may need a snippet of code to execute automatically on interval. In Laravel you can create your own command to do just that.

In this example we will send email notifications to a user every hour. However, you can still follow along if your cron job will do something different. This tutorial will not cover how to create emails in laravel.

You can view this https://wecode101.com/Laravel-8-mailgun-receive-mail tutorial in order to understand how.

Let us look at how to create a cron job using Laravel 9.

Step 1. Execute command to create new command

php artisan make:command EmailNotification

You can call the file whatever name is best, based on what it will do.

Step 2. Edit EmailNotification.php

Go to app/Console/Commands and you will find EmailNotification.php 

Change the signature within this file that best describes the task:

Change

protected $signature = 'command:name';

to

protected $signature = 'email:notification';

Change the description within this file that best describes the task:

Change

protected $description = 'Command description';

to

protected $description = 'Send email notification';

Place the code snippet that is to be executed every hour within handle() method

/**
 * Execute the console command.
 *
 * @return int
*/
public function handle()
{
  Mail::to("user1@gmail.com")->send(new \App\Mail\Notification());
}

Step 3. Register email notification command

Go to app/Console/Kernel.php file and edit as below:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

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

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('email:notification')->hourly();
    }

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

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

Now we defined the command on Kernel.php file by adding the class in $commands array and signature in schedule() method.

Step 4. Check if command registered by running command below:

php artisan list

Step 5. Test command by running command below:

php artisan email:notification

You can also manually run command at any time in order to test everthing is working.

Here is a list of scheduling options below:

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
->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
Any question?
This field is required
Your question have been successfully submitted and will be reviewed before published
Please login to ask a question