Laravel 12 Cron Job Task Scheduling

In this guide, I’ll show you how to schedule and run cron jobs in Laravel 12. You can automate tasks like emails, reports, or cleanup using Laravel’s built-in task scheduler. No more relying on browser hits or external scripts.

What is Laravel Task Scheduling?

Laravel’s task scheduler lets you automate repetitive tasks by writing scheduled commands in code. It replaces the need for messy cron syntax.

Step-by-Step: Set Up Cron Jobs in Laravel 12

Step-by-Step: Set Up Cron Jobs in Laravel 12

 

Step 1: Install Laravel 12 Application

Install laravel 12 application using the following command.

laravel new example-app

 

Step 2: Create New Command

Run the following Artisan command to create a new console command:

php artisan make:command SendEmailReport --command=report:send

This will create a file in app/Console/Commands/SendEmailReport.php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class SendEmailReport extends Command
{
    protected $signature = 'report:send';
    protected $description = 'Send daily email report';

    public function handle()
    {
        // Your logic here
        \Log::info("Daily email report sent at " . now());
        // Or trigger a Mailable or job here
    }
}

 

Step 3: Register as Task Scheduler

routes/console.php

<?php
  
use Illuminate\Support\Facades\Schedule;
  
Schedule::command('report:send')->everyMinute();

You can customize the timing using methods like:

  • ->everyMinute()
  • ->hourly()
  • ->daily()
  • ->weekly()
  • ->cron('0 0 * * *') // custom cron expression

 

Step 4: Run Scheduler Command For Test

To test your command manually.

php artisan report:send

To test the scheduler manually.

php artisan schedule:run

 

Step 5: Laravel Cron Job Setup on Server

Open your crontab editor:

crontab -e

Add the following line:

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

Replace /path-to-your-project with your Laravel project directory.

This runs Laravel's scheduler every minute, and Laravel will decide which tasks should run.

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS