• Fri, Mar 2026

Laravel Task Scheduling with Cron Jobs: Automating Your Workflow

Laravel Task Scheduling with Cron Jobs: Automating Your Workflow

This in-depth beginner-to-advanced tutorial explains Laravel’s task scheduling system and how it integrates with cron jobs to automate repetitive tasks. You’ll learn step by step how to define schedules, build custom commands, configure cron jobs, and implement best practices for smooth automation. By the end, you’ll be confident in setting up and managing automated workflows in Laravel projects.

Introduction

Imagine you need to send daily reports to users, clean up temporary files weekly, or update data every hour. Doing this manually is not practical. Laravel provides a powerful task scheduling system that works seamlessly with cron jobs to automate repetitive tasks.

Instead of writing multiple cron entries for each task, you define them once in Laravel’s app/Console/Kernel.php. Then, with a single system cron job, Laravel handles the rest.

How Laravel Task Scheduling Works

Laravel’s scheduler is built on top of cron, the standard Unix job scheduler. Normally, you’d create multiple cron jobs for each scheduled task. But Laravel simplifies this by requiring only one cron job that runs every minute. Laravel then evaluates the schedule and runs the due tasks.


* * * * * php /path/to/laravel/artisan schedule:run >> /dev/null 2>&1
    

This cron entry tells the system to run Laravel’s scheduler every minute. Laravel checks what’s due and executes it.

Setting Up the Scheduler

Step 1: Open the Kernel

Scheduled tasks are defined inside app/Console/Kernel.php in the schedule() method.


protected function schedule(Schedule $schedule)
{
    // Schedule tasks here
}
    

Step 2: Add Your System Cron Job

Edit the crontab using:


crontab -e
    

Add this line (replace path with your project path):


* * * * * php /var/www/laravel-app/artisan schedule:run >> /dev/null 2>&1
    

Save and exit. Now Laravel’s scheduler runs automatically.

Scheduling Artisan Commands

You can schedule built-in Artisan commands. For example:


protected function schedule(Schedule $schedule)
{
    $schedule->command('inspire')->hourly();
}
    

This runs php artisan inspire once every hour.

Scheduling Shell Commands

Laravel also supports scheduling raw shell commands.


protected function schedule(Schedule $schedule)
{
    $schedule->exec('php -v')->daily();
}
    

Creating a Custom Artisan Command

Step 1: Generate Command


php artisan make:command SendReports
    

Step 2: Edit the Command

Open app/Console/Commands/SendReports.php.


namespace App\Console\Commands;

use Illuminate\Console\Command;

class SendReports extends Command
{
    protected $signature = 'reports:send';
    protected $description = 'Send daily reports to users';

    public function handle()
    {
        // Logic to send reports
        $this->info('Reports sent successfully!');
    }
}
    

Step 3: Schedule the Command


protected function schedule(Schedule $schedule)
{
    $schedule->command('reports:send')->dailyAt('08:00');
}
    

Task Frequency Options

Laravel provides expressive methods to set task frequency.

MethodFrequencyExample
->everyMinute()Once every minute$schedule->command('reports:send')->everyMinute();
->hourly()Once every hour$schedule->command('reports:send')->hourly();
->daily()Once daily at midnight$schedule->command('reports:send')->daily();
->dailyAt('13:00')Once daily at 1 PM$schedule->command('reports:send')->dailyAt('13:00');
->weekly()Once per week$schedule->command('reports:send')->weekly();
->monthly()Once per month$schedule->command('reports:send')->monthly();

Conditionally Running Tasks

You can run tasks conditionally using closures.


$schedule->command('reports:send')->daily()->when(function () {
    return date('N') < 6; // Run only on weekdays
});
    

Preventing Task Overlap

Some tasks may take longer than expected. Use withoutOverlapping() to avoid duplicate executions.


$schedule->command('reports:send')->hourly()->withoutOverlapping();
    

Sending Output to Log Files

You can redirect task output to log files for debugging.


$schedule->command('reports:send')
         ->daily()
         ->sendOutputTo(storage_path('logs/reports.log'));
    

Running Tasks in Background

If you don’t want a task to block others, use runInBackground().


$schedule->command('reports:send')->everyMinute()->runInBackground();
    

Testing Scheduled Tasks

Run scheduler manually to test:


php artisan schedule:run
    

This will execute any due tasks at the current time.

Best Practices for Task Scheduling

  • Keep scheduled commands idempotent (safe to run multiple times).
  • Log task results for debugging and monitoring.
  • Use queues for long-running tasks inside scheduled commands.
  • Test tasks locally before deploying to production.
  • Monitor cron jobs with server logs or monitoring tools.

Conclusion

Laravel’s task scheduling system combined with cron jobs is a game-changer for developers. It eliminates the need to manually manage multiple cron entries and provides a clean, expressive API for defining tasks. With scheduled commands, conditional logic, overlapping prevention, and logging, Laravel gives you all the tools to automate workflows effectively.

By implementing the steps in this tutorial, you can set up a reliable task automation system for your Laravel applications, saving time and reducing human error.

This website uses cookies to enhance your browsing experience. By continuing to use this site, you consent to the use of cookies. Please review our Privacy Policy for more information on how we handle your data. Cookie Policy