Laravel Command Scheduler
Code-first command scheduling framework for Laravel
Define schedules directly on Artisan command classes using PHP 8 attributes, persist schedule definitions to the database, enforce run windows, and control tasks via a Livewire dashboard or CLI.
Requirements
- PHP ^8.4
- Laravel ^12.0 | ^13.0
- Livewire ^3.0 or ^4.0
- MaryUI ^2.1
Install with Composer
composer require t-labs-co/command-schedulerLaravel Command Scheduler lets you declare a command’s cron and run window with a #[Scheduled] attribute right on the Artisan class. Schedules sync to the database, so you can toggle a task off without a deploy. Every run is logged with duration, memory, and exit code, and an optional Livewire dashboard lets you monitor and trigger tasks.
Why not routes/console.php?
Laravel’s built-in scheduler is solid, and for a handful of static tasks it’s all you need. This package is for when schedules become something you manage, not just something you declare.
| routes/console.php (native) | Command Scheduler | |
|---|---|---|
| Define recurring tasks | ✅ Schedule::command() |
✅ #[Scheduled] attribute |
| Where the schedule lives | Central file, separate from the command | On the command class itself |
| Frequency, windows, timezone, hooks | ✅ Fluent methods | ✅ Cron plus 14 built-in run windows |
| Overlap and single-server locks | ✅ withoutOverlapping(), onOneServer() |
Native locks still apply |
| Enable or disable at runtime | Needs a code change and deploy, or custom flags | ✅ Database toggle, no deploy |
| Run history | Not stored by default | ✅ cs_scheduled_command_runs |
| Web UI | None built in | ✅ Optional Livewire dashboard |
| Extra dependencies | None | Package, plus Livewire and MaryUI for the dashboard |
Choose native if your tasks are few and rarely change. Choose this package if you want schedules defined with the command, controlled at runtime, and tracked with run history.
Get started in minutes
Follow these steps to install the package, define your first scheduled command, and verify it works.
1. Install the package
composer require t-labs-co/command-scheduler
This adds the package to your composer.json and installs all dependencies.
2. Run the required database migrations
php artisan migrate
The package creates two tables: cs_scheduled_tasks for storing discovered schedule definitions, and cs_scheduled_command_runs for tracking execution history. You should see both tables created in your database.
3. Define a scheduled Artisan command
Add the #[Scheduled] attribute to any existing Artisan command:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use TLabsCo\CommandScheduler\Attributes\Scheduled;
use TLabsCo\CommandScheduler\Core\Scheduling\Domain\Enums\RunWindow;
#[Scheduled(
cron: '*/15 * * * *',
window: RunWindow::WORKING_HOURS,
category: 'maintenance',
description: 'Clean up temporary files during working hours'
)]
class PruneTempFilesCommand extends Command
{
protected $signature = 'app:prune-temp';
protected $description = 'Clean up temporary files';
public function handle(): int
{
// Your cleanup logic here
return self::SUCCESS;
}
}
The attribute declares the schedule directly on the command class, so the scheduling rule stays close to the implementation.
4. Synchronize discovered commands
php artisan scheduler:sync
This scans your command directories, discovers all commands with #[Scheduled] attributes, and persists their definitions to the database. Run this command after adding or modifying scheduled commands.
5. List registered tasks and run one manually
# See all discovered tasks and their status
php artisan scheduler:list
# Run a specific task immediately
php artisan scheduler:run app:prune-temp --force
The list command shows all registered tasks, their cron expressions, run windows, and whether they’re enabled in the database. The run command executes a task immediately, bypassing run window checks when --force is used.
6. Configure recurring execution
The package provides its own scheduler that runs discovered tasks according to their schedule. Add this to your production environment’s cron or process manager to execute tasks on their defined schedule.
For the complete implementation details, see the Usage Guide.
Features
Code-first scheduling
Define schedules right on Artisan command classes using #[Scheduled] PHP 8 attributes or the SchedulableCommand interface. This keeps scheduling rules close to command implementations, eliminating the need to maintain a separate schedule file.
Run windows
Restrict command execution to specific business windows without writing complex cron expressions. Built-in windows include:
- WORKING_HOURS — Runs only during configured business hours
- WEEKEND — Runs on Saturdays and Sundays
- HOLIDAY — Runs on configured holiday dates
- TRADING — Stock market trading hours
- PREMARKET — Stock market pre-market hours
And more. See the Usage Guide for all 14 run window types.
Database runtime controls
All discovered tasks are synced to the cs_scheduled_tasks table, allowing administrators to toggle tasks on/off or adjust parameters at runtime without redeploying code.
Execution tracking
Every execution is automatically logged to cs_scheduled_command_runs with start time, completion time, duration, peak memory usage, and exit code. Use this data to monitor performance and debug failures.
Dashboard
The package includes a Livewire/MaryUI dashboard for monitoring scheduled tasks and managing executions from a web interface.
What the dashboard shows
- Upcoming runs — See which tasks are scheduled to run next and when
- Execution logs — Inspect recent runs with duration, memory usage, and exit codes
- Task management — Toggle tasks on/off and trigger manual runs
- Status overview — Quick view of all registered tasks and their current state
Dashboard requirements
The dashboard is optional and requires additional dependencies beyond the core package:
- Livewire
^3.0or^4.0 - MaryUI
^2.1
Installing the dashboard
composer require livewire/livewire
composer require robsontenorio/mary
Then build your frontend assets:
npm run build
The dashboard is accessible by default at /admin/scheduler and /scheduler.
Configuring access permissions
Important: The dashboard exposes controls for manually executing commands and toggling task schedules. Configure the authorization gate deliberately to prevent unauthorized access.
In your AppServiceProvider or AuthServiceProvider:
use Illuminate\Support\Facades\Gate;
public function boot(): void
{
Gate::define('viewSchedulerDashboard', function ($user) {
return $user->hasRole('admin');
});
}
Do not expose manual execution controls to unauthorized users, as they can trigger any registered command.
Troubleshooting
Dashboard route not found: Ensure Vite assets are built (npm run build) and that Livewire routes are registered. Check your vite.config.js for proper asset configuration.
Missing assets: If the dashboard loads but styles are broken, verify that MaryUI Tailwind plugins are configured in your tailwind.config.js.
For a complete step-by-step guide, see the Dashboard Setup Guide.
Artisan commands reference
| Command | Description |
|---|---|
php artisan scheduler:sync |
Scan command directories and sync definitions to the database |
php artisan scheduler:list |
Display all discovered tasks and their database runtime status |
php artisan scheduler:run {command} |
Trigger execution immediately |
php artisan scheduler:prune-logs |
Delete execution history older than configured retention days |
Configuration
Publish the configuration file to customize behavior:
php artisan vendor:publish --tag="command-scheduler-config"
The configuration file allows you to set default run windows, command directories to scan, log retention periods, and other package settings.
For complete documentation, see the Usage Guide on GitHub.
Why I built it
Laravel's native scheduler typically centralizes schedules in one file, away from the commands they run. This package lets you declare schedules right on the command class, persist them to the database for runtime control, and enforce business-aware run windows — all with a dashboard to monitor and manage tasks.
Hire me for Laravel scheduling and task automation work
Need a package tailored to your application? From recurring Laravel maintenance tasks to reusable components, I can help design, build, and review production-ready solutions.
Discuss your project
Support this package