DEV Community

Aleson França
Aleson França

Posted on

Scheduling Tasks with Cronjobs in Laravel

Laravel provides an elegant way to schedule recurring tasks using Task Scheduling, eliminating the need to manually configure multiple cronjobs. In this post, we'll explore how to set up and manage these tasks efficiently.


Configuring Laravel's Scheduler

Task scheduling in Laravel is managed through the app/Console/Kernel.php file. Inside this file, we find the schedule() method, where we can define our recurring tasks.

Basic example:

protected function schedule(Schedule $schedule)
{
    $schedule->command('emails:send')->daily();
}
Enter fullscreen mode Exit fullscreen mode

This command will execute php artisan emails:send once per day.


Running the Scheduler

To ensure the scheduler works correctly, we need to set up a single cronjob on the server:

* * * * * php /path/to/project/artisan schedule:run >> /dev/null 2>&1
Enter fullscreen mode Exit fullscreen mode

This ensures that Laravel checks and runs the scheduled tasks every minute.


Scheduling Examples

  • Running a command at a specific time:
$schedule->command('backup:run')->dailyAt('02:00');
Enter fullscreen mode Exit fullscreen mode
  • Logging output to a file:
$schedule->command('emails:send')->daily()->appendOutputTo(storage_path('logs/emails.log'));
Enter fullscreen mode Exit fullscreen mode
  • Executing an external script:
$schedule->exec('node /path/to/script.js')->daily();
Enter fullscreen mode Exit fullscreen mode
  • Running a queued job periodically:
$schedule->job(new ProcessOrdersJob)->everyFiveMinutes();
Enter fullscreen mode Exit fullscreen mode
  • Running a command every hour:
$schedule->command('reports:generate')->hourly();
Enter fullscreen mode Exit fullscreen mode

Execution Constraints and Control

We can add conditions to prevent overlapping executions or set restrictions based on the environment:

Prevent overlapping:

$schedule->command('queue:work')->everyMinute()->withoutOverlapping();
Enter fullscreen mode Exit fullscreen mode

Run only in the production environment:

$schedule->command('cache:clear')->daily()->onOneServer()->environments(['production']);
Enter fullscreen mode Exit fullscreen mode

Monitoring And Debugging

To check scheduled tasks, use the command:
php artisan schedule:list

To manually run a scheduled event:
php artisan schedule:run


Conclusion

Laravel's Task Scheduling feature simplifies recurring task execution and eliminates the complexity of traditional cronjobs. With just one cronjob configured on the server, we can manage multiple tasks directly in the code, making maintenance and monitoring much easier.

By leveraging the schedule() method, we can optimize task execution while maintaining control over their frequency, logging, and dependencies. This approach is particularly useful for automating routine processes like backups, email dispatch, report generation, and more. Additionally, integrating Laravel's scheduling with queue workers can enhance system efficiency and prevent performance bottlenecks.

Do you use cronjobs in Laravel? Share your experience in the comments!

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

If this article connected with you, consider tapping ❤️ or leaving a brief comment to share your thoughts!

Okay