How to Set Up a Cron Job & Task Scheduler in Laravel

Need to run automated tasks — sending reminder emails, cleaning up old data, or publishing scheduled articles? Laravel has a Task Scheduler that keeps scheduling neat in a single f...

How to Set Up a Cron Job & Task Scheduler in Laravel
Advertisement

Need to run automated tasks — sending reminder emails, cleaning up old data, or publishing scheduled articles? Laravel has a Task Scheduler that keeps scheduling neat in a single file, without creating many separate cron jobs on the server. This article explains how to use it from scratch.

The Concept: One Cron for Everything

Instead of adding many cron entries on the server, Laravel only needs one cron that calls schedule:run every minute. Everything else is managed within your Laravel code.

Step 1: Define the Schedule

Open app/Console/Kernel.php (Laravel 10 and below) and write your schedule in the schedule() method:

Advertisement
protected function schedule(Schedule $schedule)
{
    // example: clear logs every day at 1 AM
    $schedule->command('log:clear')->dailyAt('01:00');

    // example: run a closure every hour
    $schedule->call(function () {
        // your logic here
    })->hourly();
}

Some frequently used frequencies: ->everyMinute(), ->hourly(), ->daily(), ->dailyAt('13:00'), ->weekly(), and ->cron('0 */2 * * *') for custom patterns.

Step 2: Test Locally

Run it manually to make sure your schedule is correct:

php artisan schedule:run

This command runs all tasks whose time has arrived. To view the list of registered schedules:

php artisan schedule:list

Step 3: Set Up the Cron on the Server

For it to run automatically, add a single cron job on the server. Open the crontab:

Advertisement
crontab -e

Then add the following line (adjust the project path):

* * * * * cd /var/www/laravel_app && php artisan schedule:run >> /dev/null 2>&1

Setting Up Cron in cPanel

On shared cPanel hosting, open the Cron Jobs menu, choose the "Once per minute" interval, then fill the command with your PHP and project path:

/usr/local/bin/php /home/user/laravel_app/artisan schedule:run >> /dev/null 2>&1

Real-World Example: Auto-Publishing Scheduled Content

The Task Scheduler is perfect for publishing articles that have been prepared as drafts on a specific date. For example, a command that changes an article's status to published when its time arrives, run with ->daily(). With this pattern, you can prepare a full month of content and let it publish automatically.

Conclusion

The Task Scheduler makes scheduling in Laravel centralized and easy to maintain: define the schedule in your code, then set up a single schedule:run cron on the server. After that, any task — from sending emails to auto-publishing content — can run automatically. For the server deployment itself, see our guide on how to deploy Laravel to an Ubuntu VPS with Nginx.

Advertisement
task scheduler laravel cron job laravel schedule run laravel laravel penjadwalan tugas cron laravel cpanel
Share this article
Back to Blog
🚀 Partner Recommendation

Need Premium Source Code & Business Apps?

Access Laravel applications, POS systems, School Management, Clinic Software, ERP solutions, and ready-to-use premium source code at GudangCode.

GudangCode
  • ✔ Premium Source Code
  • ✔ Ready-to-Use Systems
  • ✔ Lifetime Updates
  • ✔ Lifetime Membership
  • ✔ Daily App Updates
Join Membership →
Advertisement
Latest Articles
Oct 14, 2026
Oct 13, 2026
Oct 11, 2026
Advertisement