0
0
LaravelHow-ToBeginner · 4 min read

How to Use Artisan Schedule in Laravel for Task Automation

Use Laravel's artisan schedule by defining scheduled tasks in the app/Console/Kernel.php file inside the schedule method. Then run php artisan schedule:run via a cron job every minute to execute scheduled commands automatically.
📐

Syntax

The schedule method in app/Console/Kernel.php is where you define your scheduled tasks using the Schedule object. You can schedule commands, closures, or shell commands with timing methods like daily(), hourly(), or everyMinute().

Example parts explained:

  • $schedule->command('emails:send'): Runs the Artisan command named emails:send.
  • daily(): Runs the task once every day.
  • ->withoutOverlapping(): Prevents the task from running if the previous run is still active.
php
protected function schedule(Schedule $schedule)
{
    $schedule->command('emails:send')->daily()->withoutOverlapping();
}
💻

Example

This example schedules a command to clear application cache every hour. It shows how to define the task and how to set up the system cron to run Laravel's scheduler every minute.

php
<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('cache:clear')->hourly();
    }

    protected function commands()
    {
        $this->load(__DIR__.'/Commands');
    }
}

// System cron entry (run 'crontab -e' to add):
// * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Output
No direct output; the cache is cleared every hour automatically when the cron runs.
⚠️

Common Pitfalls

  • Not setting up the system cron: Laravel's scheduler only runs when php artisan schedule:run is executed every minute by the system cron.
  • Forgetting to register commands: Custom commands must be registered in app/Console/Kernel.php or auto-discovered.
  • Overlapping tasks: Without withoutOverlapping(), tasks may run multiple times if previous runs are slow.
bash
/* Wrong: No system cron setup means tasks never run automatically */
// No cron entry added

/* Right: Add this cron entry to run scheduler every minute */
// * * * * * cd /path-to-project && php artisan schedule:run >> /dev/null 2>&1
📊

Quick Reference

MethodDescription
command('name')Schedule an Artisan command by its name
call(Closure)Schedule a Closure to run
exec('shell command')Run a shell command
everyMinute()Run task every minute
hourly()Run task every hour
daily()Run task once daily
weekly()Run task once weekly
monthly()Run task once monthly
withoutOverlapping()Prevent overlapping runs
onOneServer()Run task only on one server in multi-server setups

Key Takeaways

Define scheduled tasks inside the schedule() method in app/Console/Kernel.php.
Set up a system cron to run 'php artisan schedule:run' every minute for automation.
Use timing methods like daily(), hourly(), or everyMinute() to control task frequency.
Prevent overlapping tasks with withoutOverlapping() to avoid conflicts.
Register custom commands properly to ensure they can be scheduled.