The scheduler helps you run tasks automatically at set times without doing it yourself.
Scheduler with cron in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
protected function schedule(Schedule $schedule)
{
$schedule->command('your:command')->cron('* * * * *');
}The cron method uses standard cron syntax: minute, hour, day, month, weekday.
Put your scheduled commands inside the schedule method in app/Console/Kernel.php.
emails:send command every day at 9:00 AM.$schedule->command('emails:send')->cron('0 9 * * *');
backup:run command every Sunday at midnight.$schedule->command('backup:run')->cron('0 0 * * 0');
cleanup:temp command every 15 minutes.$schedule->command('cleanup:temp')->cron('*/15 * * * *');
This example shows how to schedule a command named report:generate to run every day at 7 AM using cron syntax. The command prints a message when it runs.
<?php namespace AppConsole; use IlluminateConsoleSchedulingSchedule; use IlluminateFoundationConsoleKernel as ConsoleKernel; class Kernel extends ConsoleKernel { protected function schedule(Schedule $schedule): void { // Run the 'report:generate' command every day at 7 AM $schedule->command('report:generate')->cron('0 7 * * *'); } protected function commands(): void { $this->load(__DIR__.'/Commands'); } } // Example command class namespace AppConsoleCommands; use IlluminateConsoleCommand; class ReportGenerate extends Command { protected $signature = 'report:generate'; protected $description = 'Generate daily report'; public function handle(): int { // Simulate report generation $this->info('Report generated successfully.'); return 0; } }
Make sure your server runs the Laravel scheduler every minute by adding this cron entry: * * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
Use php artisan schedule:list to see all scheduled tasks.
Test your commands manually with php artisan your:command before scheduling.
The scheduler runs tasks automatically using cron timing.
Define scheduled commands inside app/Console/Kernel.php.
Set up a system cron job to trigger Laravel's scheduler every minute.
Practice
Solution
Step 1: Understand Laravel scheduler role
Laravel scheduler automates running tasks like commands or jobs at set times.Step 2: Identify cron usage
Cron expressions define when tasks run automatically without manual input.Final Answer:
To automate running tasks at specified times without manual intervention -> Option CQuick Check:
Scheduler automates tasks [OK]
- Confusing scheduler with manual command execution
- Thinking scheduler creates migrations
- Assuming scheduler generates UI code
app/Console/Kernel.php?Solution
Step 1: Identify daily midnight syntax
Laravel's dailyAt('00:00') schedules a command at midnight daily.Step 2: Check other options
everyMinute() runs every minute, cron('0 12 * * *') runs at noon, hourly() runs every hour.Final Answer:
$schedule->command('emails:send')->dailyAt('00:00'); -> Option BQuick Check:
dailyAt('00:00') = midnight daily [OK]
- Using cron('0 12 * * *') which runs at noon, not midnight
- Confusing everyMinute() with daily scheduling
- Using hourly() instead of dailyAt()
app/Console/Kernel.php:
$schedule->command('backup:run')->cron('0 3 * * 1');
When will the backup:run command execute?Solution
Step 1: Decode cron expression '0 3 * * 1'
The fields mean: minute=0, hour=3, day=any, month=any, weekday=1 (Monday).Step 2: Interpret execution time
The command runs at 3:00 AM every Monday.Final Answer:
Every Monday at 3:00 AM -> Option DQuick Check:
cron('0 3 * * 1') = Monday 3AM [OK]
- Confusing weekday 1 with Sunday
- Assuming it runs daily instead of weekly
- Misreading hour or minute fields
app/Console/Kernel.php:
$schedule->command('report:generate')->cron('60 * * * *');
What is the problem with this cron expression?Solution
Step 1: Check cron minute field
The first field is minute; valid values are 0 to 59. 60 is invalid.Step 2: Confirm other fields
Other fields are valid, but minute 60 causes syntax error.Final Answer:
Minute value 60 is invalid; minutes range from 0 to 59 -> Option AQuick Check:
Minute must be 0-59, 60 is invalid [OK]
- Using 60 as minute value
- Ignoring cron field ranges
- Assuming missing hour causes error here
app/Console/Kernel.php?Solution
Step 1: Understand conditional scheduling
Laravel scheduler allows conditional scheduling by wrapping schedule calls in if statements.Step 2: Use weekdays() and at() methods
weekdays() limits to Monday-Friday, at('18:30') sets time to 6:30 PM.Step 3: Apply feature flag condition
Check config('features.enable_task') before scheduling to enable/disable task.Final Answer:
Use if (config('features.enable_task')) { $schedule->command('task:run')->weekdays()->at('18:30'); } -> Option AQuick Check:
Conditional scheduling with if + weekdays() + at() [OK]
- Scheduling unconditionally ignoring feature flag
- Using wrong cron for weekdays
- Putting condition inside command instead of scheduler
