In this article, we will delve into the process of setting up a cron job task scheduler in Laravel 10. Our focus will be on understanding and implementing the Laravel 10 cron job task scheduler. Many scenarios require running specific code at regular intervals in Laravel 10, and manually executing these tasks each time can be cumbersome.
However, by utilizing the Laravel scheduler, we can automate and create cron jobs efficiently.
Throughout this article, we will cover various aspects of Laravel 10's cron job task scheduling, including creating and managing cron jobs. By the end, you will be well-equipped to automate scheduled tasks in your Laravel 10 application, enhancing its efficiency and reliability.
In this article, we will install laravel 10 using the following command.
composer create-project laravel/laravel laravel-10-cron-job-example
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']);
*/
}
}
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');
}
}
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
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
In conclusion, we've explored the essential process of setting up a cron job task scheduler in Laravel 10. By harnessing the power of Laravel's built-in scheduler, we've learned how to automate and manage scheduled tasks effectively.
Automating tasks at specific intervals is a common requirement in web development, and Laravel's task scheduler simplifies this process. Whether you need to run routine maintenance, generate reports, or perform any other recurring tasks, the Laravel 10 cron job task scheduler provides a convenient solution.
You might also like:
In this tutorial, I will guide you through the process of adding a date range picker component to your Angular 15 applic...
Jun-30-2023
In this article, we will see laravel 10 change column names and data types in migration. Here, we will learn about...
Apr-19-2023
In this article, we will see laravel 9 generate PDF from HTML using TCPDF. Here, we will learn how to integrate tcpdf in...
Jan-31-2023
In this tutorial, I'll walk you through the process of building a custom WebSocket server using Laravel 11 and Ratch...
Sep-23-2024