How to Schedule Tasks in Laravel: Simple Guide
In Laravel, you schedule tasks by defining them in the
app/Console/Kernel.php file inside the schedule method using the Schedule class. Then, you run the Laravel scheduler via a single cron entry that calls php artisan schedule:run every minute.Syntax
Laravel uses the schedule method in app/Console/Kernel.php to define scheduled tasks. You use the $schedule object to specify commands or closures and set their frequency.
- $schedule->command('command:name'): Runs an Artisan command.
- ->daily(), ->hourly(), ->everyMinute(): Set how often the task runs.
- ->call(function() { ... }): Runs a closure instead of a command.
Finally, you must add a cron job on your server to run php artisan schedule:run every minute to trigger Laravel's scheduler.
php
protected function schedule(Schedule $schedule) { $schedule->command('emails:send')->daily(); $schedule->call(function () { // Task code here })->hourly(); }
Example
This example schedules a custom Artisan command report:generate to run daily at midnight and a closure task to run every five minutes.
php
<?php namespace AppConsole; use IlluminateConsoleScheduling\Schedule; use IlluminateFoundationConsoleKernel as ConsoleKernel; class Kernel extends ConsoleKernel { protected function schedule(Schedule $schedule) { // Run custom command daily at midnight $schedule->command('report:generate')->dailyAt('00:00'); // Run closure every 5 minutes $schedule->call(function () { // Example task: log current time Log::info('Scheduled task ran at ' . now()); })->everyFiveMinutes(); } protected function commands() { $this->load(__DIR__.'/Commands'); } } // On your server, add this cron entry: // * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Output
No direct output; tasks run silently. Logs will show 'Scheduled task ran at [timestamp]' every 5 minutes.
Common Pitfalls
- Forgetting the cron job: Laravel scheduler needs a cron entry running
php artisan schedule:runevery minute; without it, tasks won't run. - Not registering commands: Custom commands must be registered in
app/Console/Kernel.phpor auto-discovered. - Incorrect time zones: Scheduler uses the server time zone; set
timezoneinconfig/app.phpif needed. - Running heavy tasks synchronously: Use queues or async jobs for long tasks to avoid blocking.
bash
/* Wrong: No cron job set, tasks never run */ // No server cron entry for schedule:run /* Right: Add cron job */ // * * * * * cd /path-to-project && php artisan schedule:run >> /dev/null 2>&1
Quick Reference
| Method | Description | Example Usage |
|---|---|---|
| command('name') | Schedule an Artisan command | $schedule->command('emails:send')->daily(); |
| call(closure) | Schedule a closure task | $schedule->call(function () { ... })->hourly(); |
| daily() | Run task once daily at midnight | $schedule->command('task')->daily(); |
| dailyAt('HH:MM') | Run task daily at specific time | $schedule->command('task')->dailyAt('13:00'); |
| hourly() | Run task every hour | $schedule->command('task')->hourly(); |
| everyMinute() | Run task every minute | $schedule->command('task')->everyMinute(); |
| everyFiveMinutes() | Run task every 5 minutes | $schedule->command('task')->everyFiveMinutes(); |
Key Takeaways
Define scheduled tasks inside the schedule() method in app/Console/Kernel.php using the $schedule object.
Add a single cron job on your server to run 'php artisan schedule:run' every minute to trigger all scheduled tasks.
Use built-in frequency methods like daily(), hourly(), and everyFiveMinutes() to control task timing.
Register custom Artisan commands properly to ensure they can be scheduled.
Be mindful of server time zones and long-running tasks to avoid scheduling issues.