Skip to content

Laravel cron job: how to run the scheduler on shared hosting

· The AYT team · 5 min read

Developer desk with two monitors, one showing code

A Laravel cron job is a single line in your hosting panel: * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1. It starts the scheduler once a minute, and Laravel decides which tasks are due. The tasks themselves live in your code, in routes/console.php, not in the crontab.

How the Laravel scheduler works

Before the scheduler, every periodic task had its own crontab line: one for the daily report, one for pruning old data, one for the newsletter. Laravel moves them all into the application. The server keeps one cron job, which runs php artisan schedule:run every minute. The command reads the schedule from your code and starts only the tasks due in that minute.

Since Laravel 11, the schedule lives in routes/console.php:

use Illuminate\Support\Facades\Schedule;

Schedule::command('reports:send')->dailyAt('07:00');
Schedule::command('model:prune')->daily();
Schedule::job(new PruneAbandonedCarts)->everyFifteenMinutes();

The schedule is versioned with your code. When you move the app to another server, you move only the cron line.

Step 1: find the project path and the PHP path

Cron doesn't start in your app's folder and doesn't know which PHP version your site uses. You need two paths:

  • the full path of the project, the folder that holds the artisan file (something like /home/username/app), not the public folder;
  • the path to the command-line PHP, in the version your app needs. On many servers a bare php is the system's default version, which can be older than your site's. Hosts list it in their docs; on cPanel servers it usually looks like /opt/cpanel/ea-php84/root/usr/bin/php.

If you run schedule:run with a PHP version that's too old, Composer stops the app with "Composer detected issues in your platform" and no task starts.

Step 2: add the cron job in your panel

In cPanel and similar panels, the page is called Cron Jobs.

  1. Open Cron Jobs.
  2. Set the frequency to once per minute (* * * * *).
  3. Enter the command with your own paths:
cd /home/username/app && /opt/cpanel/ea-php84/root/usr/bin/php artisan schedule:run >> /dev/null 2>&1
  1. Save.

The >> /dev/null 2>&1 part discards the output, so you don't get an email every minute. While you're testing, send the output to a file instead, such as >> storage/logs/cron.log 2>&1, and switch back to /dev/null once it works.

The panel's Cron Jobs page with a task that runs every minute

Step 3: check that your tasks run

  • php artisan schedule:list shows every task and its next run. An empty list means the schedule isn't loading, usually because of a mistake in routes/console.php.
  • php artisan schedule:run, run by hand, starts whatever is due in that minute.
  • For one specific task, add ->appendOutputTo(storage_path('logs/report.log')) and read what it printed on each run.

If your host has no SSH, you can run the commands above from a temporary cron job that writes to a log file. Laravel has a few other traps on hosting without SSH, collected in our guide to Laravel on hosting without SSH.

Common problems

Tasks run at the wrong time

The scheduler uses the timezone in config/app.php (timezone), which defaults to UTC. In winter, 07:00 UTC is 09:00 in Romania. You can set the timezone per task with ->timezone('Europe/Bucharest'), or set the schedule_timezone option in config/app.php for all of them.

Laravel's documentation warns that when daylight saving time changes, a task scheduled in a timezone that uses it may run twice or not at all. In Romania the clocks change between 03:00 and 04:00, so don't schedule anything important in that hour.

Your host only allows cron every 5 or 15 minutes

Some hosts don't allow a cron job every minute. schedule:run starts only the tasks due in the exact minute it runs, so with a 5-minute cron an ->everyMinute() task runs every 5 minutes and a ->dailyAt('07:03') task never runs. Schedule tasks only on minutes that match the cron interval (00, 05, 10…).

A long task starts more than once

By default Laravel starts a task even if its previous run hasn't finished. Add ->withoutOverlapping(). The lock is kept in the cache and expires after 24 hours by default. If the process was killed and the task never starts again, clear the lock with php artisan schedule:clear-cache.

Tasks in the same minute wait for each other

Tasks scheduled for the same minute run one after another. A 50-second task delays all the others. For long tasks, add ->runInBackground() so they start separately.

schedule:work instead of cron

php artisan schedule:work keeps the scheduler running in the foreground and is meant for your own computer. On hosting it stops when the session ends, so on a server you use cron.

The scheduler on AYT hosting

On AYT, a site set up with the Laravel Toolkit doesn't need a hand-written cron line. Open the site in the Laravel Toolkit, go to Processes and turn on the Scheduler switch. It adds the cron job for php artisan schedule:run every minute, with the site's path and PHP version.

The Laravel Toolkit's Processes page with the Scheduler switch on

The same page starts queue workers if your app uses jobs; the details are in our article on Laravel queues on hosting. For a one-off command such as schedule:list, use the Commands page, which runs non-interactive artisan commands from the panel without SSH. For other periodic tasks, the account also has a Cron Jobs page. The rest of the Laravel tools are described under apps.

More articles

All articles