Task Scheduling in Laravel 12
Laravel Tip: Automate Repetitive Jobs with Task Scheduling (Laravel 12 + Shared Hosting)
Running scripts manually every day is a pain. Laravel’s Task Scheduler lets you run code automatically—hourly, daily, or whenever you choose—using just one cron entry.
1. Set a Single Cron
On the server (or cPanel), run:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
2. Define Scheduled Tasks (Laravel 12 Way)
Instead of Kernel.php, define schedules in routes/console.php:
use Illuminate\Support\Facades\Schedule;
Schedule::call(function () {
\App\Jobs\SendReport::dispatch(); // send daily report
})->dailyAt('09:00');
Schedule::command('logs:clean') // clean logs every Sunday
->weeklyOn(0, '23:30');
Schedule::call(fn () => Http::get('https://myapp.com/ping'))
->everyTenMinutes();
Laravel supports:
->everyMinute()->hourly()->dailyAt('13:00')->weeklyOn(1, '08:00')->monthly()- …and more.
3. Run Scheduler on Shared Hosting (cPanel)
Even without root access, Cron Jobs in cPanel make it easy:
- cPanel → Cron Jobs.
- Time: set Every Minute (
* * * * *). - Command: adjust for your path
/usr/bin/php /home/username/your-project/artisan schedule:run >> /dev/null 2>&1
or, if inside public_html:
/usr/bin/php /home/username/public_html/artisan schedule:run >> /dev/null 2>&1
Save — Laravel now “listens” every minute and fires tasks when due.
Tip: Some hosts limit per-minute cron; pick the smallest allowed (e.g., 5 minutes).
To debug, remove >> /dev/null 2>&1 so output goes to email or a log.Why It’s Great
- One cron entry runs all recurring tasks
- Cleaner, code-driven scheduling (no messy raw cron math)
- Works with closures, jobs, or Artisan commands