Scheduled tasks
Run a command on a timetable, like cron. Up to 10 per app or site, times in UTC, at most an hour each, with the output of the last 20 runs.
Updated 17 Oct 20263 min read
A scheduled task runs a command on a timetable, like a cron job: Laravel's scheduler every minute, a report every night, a clean-up every Sunday. Each run starts in its own short-lived sandbox, from your app's live version with its files and variables, and stops when the command ends. An app or a site can have up to 10.
Add a task
Open the app's page. Under Processes, choose Add a worker or scheduled task. On a site's page, choose Add a scheduled task.
On an app, choose Scheduled task as its Kind.
Give it a Name and a Command.
Write its Timetable in cron's five fields.
Choose Save and restart.
The timetable
Five fields, as cron writes them: minute, hour, day of the month, month, day of the week. Times are always in UTC.
| Timetable | Runs |
|---|---|
* * * * * | Every minute, the most often a task can run |
*/5 * * * * | Every 5 minutes |
0 3 * * * | Every day at 03:00 UTC |
30 21 * * 1-5 | Weekdays at 21:30 UTC |
0 0 1 * * | The first of every month at midnight UTC |
Each field takes a number, *, a range (1-5), a list (1,15) and steps (*/10).
Asha wants a sales report at 06:00 her time, which is 3 hours ahead of UTC. She writes 0 3 * * *. A visitor's time zone, or the PHP setting date.timezone, doesn't change when a task runs.
What each run does
Still running at its next time: that run is skipped, so two copies never overlap.
Over an hour: the run is stopped, and shows Stopped after an hour.
Its end: Finished, or Failed with code and the command's exit code.
Its output: the last 20 runs are kept with their last lines, and everything a task prints is in the project's logs too.
A task pays for the memory and processor time it uses while it runs, like the app's other processes.
Example: Asha's tasks
Asha's Laravel shop has two:
| Name | Timetable | Command |
|---|---|---|
scheduler | * * * * * | php artisan schedule:run |
report | 0 2 * * * | php artisan reports:nightly |
The first runs Laravel's own scheduler, which then runs whatever her code schedules: that's the usual way for Laravel, one task and the rest in routes/console.php. The second runs a command of her own straight from the timetable, and its runs show apart from the scheduler's.
For WordPress and PHP sites
A site's tasks run in the site's own sandbox, with its files and variables, like cPanel's cron jobs:
php scripts/report.phpEvery WordPress site gets WordPress's own tasks by itself: php wp-cron.php every 5 minutes. WordPress's built-in cron is switched off, so no visitor's page waits for it. See WordPress.
Sleeping apps
Tasks run on time even while an app sleeps: each run has its own sandbox, so it doesn't wake the web process.
Tasks or a worker?
Use a task for work that starts, finishes and can wait for its time. Use a worker for work that should start the moment it arrives, like a queue. Laravel apps usually have both: a worker for the queue, and the scheduler as a task.