How to Create Schedule Command in Laravel 11

Learn how to create and schedule custom commands in Laravel 11 with this detailed guide. This tutorial covers the installation of Laravel 11, creating a custom command, registering and scheduling it, and setting up a cron job on your server. Enhance your Laravel applications by automating tasks with cron jobs.

Laravel 11 brings powerful features and improvements for managing background tasks and automating processes. This comprehensive guide will walk you through creating custom commands, scheduling them, and setting up cron jobs on your server to ensure your Laravel application runs smoothly.

In Laravel 11, the process of setting up cron job task scheduling commands has changed.

The Kernel.php file, traditionally used for this purpose, has been removed.

Instead, cron jobs are now defined directly in the console.php file. Below, you’ll find a straightforward example illustrating how to create a new command and configure it within Laravel 11

So, let’s see the laravel 11 scheduler command, laravel scheduler.

Step 1: Install Laravel 11

To start, you need a Laravel 11 project. Open your terminal and run the following command to install Laravel 11:

 composer create-project laravel/laravel example-app 

This command sets up a new Laravel 11 project in a directory named example-app. Once the installation is complete, you can proceed to create your custom command.

Step 2: Create a Custom Command

Laravel’s Artisan CLI makes it easy to create custom commands. Use the following command to generate a new command class:

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

This command creates a new command class named TestCron in the app/Console/Commands directory. Open the generated file TestCron.php and add the following code to define your command’s functionality:

Step 3: Register the Task Scheduler

To ensure your custom command runs automatically, you need to schedule it. Open the routes/console.php file and add the following code to register your command:


use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;

Artisan::command('test:cron')->describe('Run the test:cron command');

Schedule::command('test:cron')->everyFiveMinutes();

 

This code schedules the test:cron command to run every five minutes.

Step 4: Run the Scheduler Command Locally

To test your cron job locally, you can manually run the scheduler with the following command:

 php artisan schedule:run

If the command executes successfully, you should see log entries in the storage/logs/laravel.log file indicating that the cron job ran at the specified time:


[2024-04-10 23:45:03] local.INFO: Cron Job running at 2024-04-10 23:45:03
[2024-04-10 23:50:05] local.INFO: Cron Job running at 2024-04-10 23:50:05
[2024-04-10 23:55:04] local.INFO: Cron Job running at 2024-04-10 23:55:04

Step 5: Set Up a Cron Job on the Server

To automate your scheduled tasks on a server, set up a cron job. For an Ubuntu server, use the following command:

 crontab -e 

Add the following line to the crontab file:

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

This cron job will run the php artisan schedule:run command every minute. Replace /path-to-your-project with the actual path to your Laravel project. Laravel will handle executing the appropriate scheduled tasks.

Summary

This cron job will run the php artisan schedule:run command every minute. Replace /path-to-your-project with the actual path to your Laravel project. Laravel will handle executing the appropriate scheduled tasks.

You may also find interesting:

Custom Middleware in Laravel 11

Post a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *