0
0
Laravelframework~10 mins

Scheduler with cron in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Scheduler with cron
Define Scheduled Task
Add Task to Kernel Schedule()
Set Cron Job on Server
Cron Runs Every Minute
Laravel Checks Scheduled Tasks
Run Due Tasks
Task Executes
Wait for Next Minute
Back to Cron Runs Every Minute
The flow shows how you define tasks in Laravel, set up a cron job to run every minute, and Laravel runs the scheduled tasks when due.
Execution Sample
Laravel
protected function schedule(Schedule $schedule)
{
    $schedule->command('emails:send')->dailyAt('13:00');
}
This code schedules a command to run daily at 1 PM.
Execution Table
StepActionTimeTask Due?Task Run?
1Cron runs12:59NoNo
2Cron runs13:00YesYes - emails:send command runs
3Cron runs13:01NoNo
4Cron runs13:02NoNo
5Cron runs13:03NoNo
💡 Cron runs every minute; task runs only at scheduled time 13:00
Variable Tracker
VariableStartAfter 12:59After 13:00After 13:01Final
Current Time12:5812:5913:0013:0113:03
Task DueFalseFalseTrueFalseFalse
Task RunNoNoYesNoNo
Key Moments - 3 Insights
Why doesn't the task run at 12:59 even though cron runs every minute?
Because the task is scheduled to run daily at 13:00 only. At 12:59, the task is not due, so Laravel skips running it (see execution_table step 1).
How does Laravel know when to run the task?
Laravel checks the current time each minute when cron triggers it. If the current time matches the scheduled time (13:00), it runs the task (see execution_table step 2).
What happens if the cron job is not set up on the server?
Laravel's scheduler won't run automatically because cron triggers Laravel's schedule:run command every minute. Without cron, scheduled tasks never execute.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'Task Run' at step 3 (time 13:01)?
AYes
BNo
CMaybe
DError
💡 Hint
Check the 'Task Run?' column at step 3 in the execution_table.
At which step does the task become due and run?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look for 'Task Due?' and 'Task Run?' columns in execution_table.
If the cron job runs every 5 minutes instead of every minute, how would the execution_table change?
ATask runs multiple times per minute
BTask runs every minute regardless
CTask runs only if cron runs at 13:00 exactly
DTask never runs
💡 Hint
Think about how often Laravel checks scheduled tasks based on cron frequency.
Concept Snapshot
Laravel Scheduler with cron:
- Define tasks in app/Console/Kernel.php schedule() method
- Set server cron to run 'php artisan schedule:run' every minute
- Laravel checks tasks each minute and runs those due
- Tasks run only at their scheduled times
- Cron triggers Laravel scheduler, without cron tasks won't run
Full Transcript
In Laravel, you define scheduled tasks inside the schedule() method of the Console Kernel. You then set up a cron job on your server to run the command 'php artisan schedule:run' every minute. Each time cron runs, Laravel checks if any scheduled tasks are due to run at that current time. If a task is due, Laravel runs it; otherwise, it waits for the next minute. For example, a task scheduled to run daily at 13:00 will only run when the cron triggers at exactly 13:00. If cron is not set up, scheduled tasks will never run automatically. This setup allows Laravel to manage task scheduling efficiently using a single cron entry.