Laravel Task Scheduling

Laravel Task Scheduling

Laravel's task scheduler lets you define recurring jobs in code rather than managing multiple cron entries on the server.

1 - Defining Scheduled Tasks

// routes/console.php  (Laravel 11)
use Illuminate\Support\Facades\Schedule;

Schedule::command('report:send --type=summary')->dailyAt('08:00');
Schedule::command('inspire')->hourly();
Schedule::job(new PruneOldData)->weekly();
Schedule::call(fn () => DB::table('logs')->where('created_at', '<', now()->subMonth())->delete())->monthly();

2 - Frequency Options

->everyMinute()
->everyFiveMinutes()
->hourly()
->hourlyAt(17)          // at 17 minutes past the hour
->daily()
->dailyAt('13:00')
->weeklyOn(1, '8:00')  // every Monday at 8am
->monthly()
->cron('0 9 * * 1-5')  // custom cron expression

3 - Constraints and Hooks

Schedule::command('report:send')
    ->daily()
    ->environments(['production'])   // only run in production
    ->withoutOverlapping()            // skip if previous run still active
    ->onSuccess(fn () => Log::info('Report sent.'))
    ->onFailure(fn () => Log::error('Report failed.'));

4 - Running the Scheduler

// Add ONE cron entry to your server — that's all
* * * * * cd /var/www/myapp && php artisan schedule:run >> /dev/null 2>&1

// Run the scheduler locally (checks every minute)
php artisan schedule:work

// List upcoming scheduled tasks
php artisan schedule:list