Bird
Raised Fist0
Laravelframework~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of Laravel's scheduler when using cron expressions?
easy
A. To create database migrations automatically
B. To manually execute commands from the terminal
C. To automate running tasks at specified times without manual intervention
D. To generate user authentication scaffolding

Solution

  1. Step 1: Understand Laravel scheduler role

    Laravel scheduler automates running tasks like commands or jobs at set times.
  2. Step 2: Identify cron usage

    Cron expressions define when tasks run automatically without manual input.
  3. Final Answer:

    To automate running tasks at specified times without manual intervention -> Option C
  4. Quick Check:

    Scheduler automates tasks [OK]
Hint: Scheduler automates tasks on schedule, not manual commands [OK]
Common Mistakes:
  • Confusing scheduler with manual command execution
  • Thinking scheduler creates migrations
  • Assuming scheduler generates UI code
2. Which of the following is the correct way to schedule a command to run every day at midnight in Laravel's app/Console/Kernel.php?
easy
A. $schedule->command('emails:send')->hourly();
B. $schedule->command('emails:send')->dailyAt('00:00');
C. $schedule->command('emails:send')->cron('0 12 * * *');
D. $schedule->command('emails:send')->everyMinute();

Solution

  1. Step 1: Identify daily midnight syntax

    Laravel's dailyAt('00:00') schedules a command at midnight daily.
  2. Step 2: Check other options

    everyMinute() runs every minute, cron('0 12 * * *') runs at noon, hourly() runs every hour.
  3. Final Answer:

    $schedule->command('emails:send')->dailyAt('00:00'); -> Option B
  4. Quick Check:

    dailyAt('00:00') = midnight daily [OK]
Hint: dailyAt('00:00') means every day at midnight [OK]
Common Mistakes:
  • Using cron('0 12 * * *') which runs at noon, not midnight
  • Confusing everyMinute() with daily scheduling
  • Using hourly() instead of dailyAt()
3. Given this scheduler code in app/Console/Kernel.php:
$schedule->command('backup:run')->cron('0 3 * * 1');
When will the backup:run command execute?
medium
A. Every hour on the hour
B. Every day at 3:00 AM
C. Every Sunday at 3:00 AM
D. Every Monday at 3:00 AM

Solution

  1. Step 1: Decode cron expression '0 3 * * 1'

    The fields mean: minute=0, hour=3, day=any, month=any, weekday=1 (Monday).
  2. Step 2: Interpret execution time

    The command runs at 3:00 AM every Monday.
  3. Final Answer:

    Every Monday at 3:00 AM -> Option D
  4. Quick Check:

    cron('0 3 * * 1') = Monday 3AM [OK]
Hint: Cron weekday 1 means Monday at specified time [OK]
Common Mistakes:
  • Confusing weekday 1 with Sunday
  • Assuming it runs daily instead of weekly
  • Misreading hour or minute fields
4. You wrote this in app/Console/Kernel.php:
$schedule->command('report:generate')->cron('60 * * * *');
What is the problem with this cron expression?
medium
A. Minute value 60 is invalid; minutes range from 0 to 59
B. Hour value is missing
C. Day of month is invalid
D. Cron expression is missing the command name

Solution

  1. Step 1: Check cron minute field

    The first field is minute; valid values are 0 to 59. 60 is invalid.
  2. Step 2: Confirm other fields

    Other fields are valid, but minute 60 causes syntax error.
  3. Final Answer:

    Minute value 60 is invalid; minutes range from 0 to 59 -> Option A
  4. Quick Check:

    Minute must be 0-59, 60 is invalid [OK]
Hint: Minutes in cron go 0-59, never 60 [OK]
Common Mistakes:
  • Using 60 as minute value
  • Ignoring cron field ranges
  • Assuming missing hour causes error here
5. You want to schedule a Laravel command to run every weekday at 6:30 PM but only if a certain feature flag is enabled. Which is the best way to implement this in app/Console/Kernel.php?
hard
A. Use if (config('features.enable_task')) { $schedule->command('task:run')->weekdays()->at('18:30'); }
B. Schedule the command daily at 18:30 without condition
C. Use cron expression '30 18 * * 0-6' to run every day
D. Add the condition inside the command handle method instead of scheduler

Solution

  1. Step 1: Understand conditional scheduling

    Laravel scheduler allows conditional scheduling by wrapping schedule calls in if statements.
  2. Step 2: Use weekdays() and at() methods

    weekdays() limits to Monday-Friday, at('18:30') sets time to 6:30 PM.
  3. Step 3: Apply feature flag condition

    Check config('features.enable_task') before scheduling to enable/disable task.
  4. Final Answer:

    Use if (config('features.enable_task')) { $schedule->command('task:run')->weekdays()->at('18:30'); } -> Option A
  5. Quick Check:

    Conditional scheduling with if + weekdays() + at() [OK]
Hint: Wrap schedule call in if to conditionally enable task [OK]
Common Mistakes:
  • Scheduling unconditionally ignoring feature flag
  • Using wrong cron for weekdays
  • Putting condition inside command instead of scheduler