When working on web applications, automation saves time and reduces manual errors. Laravel provides a powerful task scheduling feature that allows you to define repetitive tasks inside your application. In this article, we will learn how to set up Laravel scheduler with crontab on an Ubuntu system. This guide explains real-world use cases, the installation process, how to configure scheduling commands, and setting up the crontab in the system for smooth integration.
Use cases of cron functionality in Laravel
Cron is a time-based job scheduler in Unix-like operating systems that helps automate repetitive tasks. When combined with Laravel, cron becomes a powerful tool to handle routine operations without manual effort. Instead of remembering to run commands yourself, the system takes care of executing them at the exact time you define. This automation improves efficiency, saves developer time, and ensures consistency in application behavior.
Here are some of the most common and widely used tasks configured in crontab with Laravel:
- Running daily reports for analytics or sales summaries
- Sending email notifications to users or admins
- Scheduling backups for databases or files
- Triggering reminders such as pending payments or upcoming events
- Performing resource cleanups like clearing temporary files, cache, or logs
Installing crontab on Ubuntu
Before configuring the Laravel scheduler, we must ensure crontab is installed on the Ubuntu system. If you have not installed it yet, you can follow our detailed guide in the post “How to Install Crontab in Ubuntu“. That article will help you set up crontab properly on your server or local development system so you can continue with the Laravel scheduler integration smoothly.
Scheduling command in Laravel
Laravel makes scheduling tasks very straightforward. All you need to do is define your scheduled commands inside the routes/console.php file. This allows you to automate repetitive jobs such as sending reports, clearing logs, or running system backups at specific times.
Here are some real examples of scheduled commands you can add in routes/console.php:
use Illuminate\Support\Facades\Schedule;
Schedule::command('report:generate')->dailyAt('01:00');
Schedule::command('emails:send')->hourly();
Schedule::command('backup:run')->weeklyOn(1, '02:30');
Each line specifies when the command should run, and Laravel takes care of the rest. This makes it easy to build automation directly into your application workflow.
If you want to build your own artisan commands for specific business requirements, be sure to read our detailed guide Laravel Custom Artisan Command Example, where we show step by step how to generate and manage custom commands effectively.
Setting up crontab in system
Once your Laravel commands are scheduled inside the application, the next step is to configure the system crontab so it can trigger the Laravel scheduler automatically. Open the crontab editor using the command:
crontab -e
Now add the following line at the end of the file. Be sure to replace the path with the location of your Laravel project:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
This tells the system to run the Laravel scheduler every minute, which checks and runs any tasks you have scheduled in your Laravel application. If you notice that scheduled tasks are not running, you can temporarily remove the
part and check the output for debugging.>> /dev/null 2>&1
While setting up Laravel scheduler with crontab, many developers face common issues. Sometimes the PHP path is not correct, leading to commands not running as expected. Other times, the crontab is set under the wrong user account, causing permission problems. Make sure the project path is accurate and confirm that the Laravel environment variables are correctly loaded.
Conclusion
The process of how to set up Laravel scheduler with crontab is straightforward and extremely useful for automating tasks. From sending daily reports to maintaining backups, cron integration in Laravel can save time and improve reliability. By carefully installing crontab, scheduling your commands, and configuring the system crontab, you can achieve seamless automation.