Laravel Task Scheduling: Beyond Basic Cron
Laravel Task Scheduling: Beyond Basic Cron
Laravel Task Scheduling: Beyond Basic Cron
Tip: Prevent Overlapping Tasks
$schedule->command('reports:generate')->everyFiveMinutes()->withoutOverlapping();
If the previous run is still executing, skip this one.
Gotcha: withoutOverlapping() Uses Cache
It creates a lock in the cache. If your cache driver is down, overlapping prevention fails silently.
Tip: Run on One Server Only
$schedule->command('reports:generate')->daily()->onOneServer();
Critical for load-balanced apps where multiple servers run the same cron.
Gotcha: Timezone Confusion
$schedule->command('reports:generate')
->dailyAt('09:00')
->timezone('America/New_York');
Without explicit timezone, it uses the app's default.
Tip: Send Output to Email
$schedule->command('reports:generate')
->daily()
->emailOutputTo('admin@example.com');
Only works with sendOutputTo() first.
Gotcha: Maintenance Mode Skips Scheduled Tasks
Tasks don't run during maintenance mode unless you add ->evenInMaintenanceMode().
Tip: Use route:cache Carefully
php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.
Tip: Model APP_KEY Rotation
Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.
Gotcha: Local Scope Leaks
Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.
Senior Insight
Task scheduling in Laravel is elegant, but the default everyMinute() cron entry is a footgun. On a high-traffic app, running php artisan schedule:run every minute means 1,440 PHP processes per day just to check if something needs to run. I stagger less frequent tasks with ->when() closures to minimize overhead, and I always use ->withoutOverlapping() on any task that touches shared resources — disk space monitoring excluded, ironically.
Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)