php
39 lines · 7 steps
How task scheduling works in Laravel
Laravel's console Kernel registers recurring jobs with fluent frequency and safety constraints in one place.
Explained by
highlit
1<?php
2
3namespace App\Console;
4
5use Illuminate\Console\Scheduling\Schedule;
6use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7
8class Kernel extends ConsoleKernel
9{
10 protected function schedule(Schedule $schedule): void
11 {
12 $schedule->command('sessions:gc')
13 ->hourly();
14
15 $schedule->command('telescope:prune --hours=48')
16 ->daily();
17
18 $schedule->command('cleanup:stale-records --days=30')
19 ->dailyAt('02:30')
20 ->timezone('America/New_York')
21 ->withoutOverlapping()
22 ->onOneServer()
23 ->runInBackground()
24 ->emailOutputOnFailure(config('mail.admin_address'))
25 ->appendOutputTo(storage_path('logs/cleanup.log'));
26
27 $schedule->call(function () {
28 \App\Models\TemporaryUpload::where('created_at', '<', now()->subDay())
29 ->each(fn ($upload) => $upload->delete());
30 })->dailyAt('03:00')->name('prune-temp-uploads')->withoutOverlapping();
31 }
32
33 protected function commands(): void
34 {
35 $this->load(__DIR__ . '/Commands');
36
37 require base_path('routes/console.php');
38 }
39}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Defining all recurring work in one schedule method gives you a single source of truth instead of scattered crontab entries.
- 2Fluent constraints like withoutOverlapping and onOneServer make scheduled tasks safe to run across multiple servers.
- 3You can schedule existing Artisan commands or inline closures, whichever fits the job.
Related explainers
php
<?php namespace App\Support;
Locale-aware formatting with PHP's intl extension
internationalization
encapsulation
constructor-injection
Intermediate
7 steps
php
<?php namespace App\Support;
Merging query params onto a URL in PHP
url-parsing
query-strings
immutability
Intermediate
8 steps
php
<?php class ImageUploadService {
Validating file uploads safely in PHP
file-upload
input-validation
security
Intermediate
8 steps
php
<?php namespace App\View;
Building a safe HTML escaper in PHP
security
xss
escaping
Intermediate
6 steps
php
<?php namespace App\Observers;
How Eloquent observers hook lifecycle events in Laravel
observers
lifecycle-hooks
queues
Intermediate
6 steps
ruby
class ReportBatcher BATCH_SIZE = 500 def initialize(account)
Batching monthly email summaries in Rails
batching
service-object
background-jobs
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-task-scheduling-works-in-laravel-explained-php-aa8e/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.