In this article, we'll explore how to set up a cron job task scheduler in Laravel 10. We'll focus on understanding and implementing the Laravel 10 cron job task scheduler. Using Laravel scheduler, we can automate and create cron jobs.
Laravel's command scheduler offers a fresh approach to managing scheduled tasks on your server. Task schedule is defined in the app/Console/Kernel.php
file's schedule
method.
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 2024-04-04 04:00:02
[2023-03-14 05:00:01] local.INFO: Cron job running at 2024-04-04 05:00:01
[2023-03-14 06:00:02] local.INFO: Cron job running at 2024-04-04 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
You might also like:
- Read Also: How To Integrate Paypal Payment Gateway In Laravel 10
- Read Also: How To Create CRUD Operation In Laravel 10 Livewire
- Read Also: Laravel 10 AJAX CRUD Operations With Popup Modal
- Read Also: Laravel 10 Toastr Notification Example