- How do I schedule an Artisan command to run every 30 seconds in Laravel?
- Use the `ShortSchedule` facade in `routes/console.php` or the `shortSchedule()` method in your `Console/Kernel.php`. Call `everySeconds(30)` on your command definition, like `ShortSchedule::command('analytics:process')->everySeconds(30)`.
- Does spatie/laravel-short-schedule work with Laravel 11?
- No, Laravel 11+ includes native sub-minute scheduling. This package is only needed for Laravel 9–13 (excluding 10/11). Check the [Laravel docs](https://laravel.com/docs/11.x/scheduling#sub-minute-scheduled-tasks) for alternatives.
- Can I run commands at sub-second intervals (e.g., every 0.5 seconds)?
- Yes, the package supports fractional seconds. Use `everySeconds(0.5)` to run a command every half-second. This is useful for high-frequency tasks like real-time IoT monitoring or live analytics.
- How do I install and set up spatie/laravel-short-schedule?
- Run `composer require spatie/laravel-short-schedule`, then add the `shortSchedule()` method to your `Console/Kernel.php` or define tasks in `routes/console.php`. Finally, run `php artisan short-schedule:run` to start the scheduler.
- Will this package block my Laravel application or use excessive resources?
- No, it runs in a separate process using Symfony’s `Process` component, avoiding blocking. However, for long-running tasks, monitor memory usage and set up a process manager like Supervisor to enforce restarts (e.g., every 60 seconds).
- Can I schedule commands conditionally (e.g., only on certain days or environments)?
- Yes, use constraints like `when()`, `between()`, or `environments()`. For example, `ShortSchedule::command('backup')->everyMinute()->when(fn() => config('app.environment') === 'production')`.
- How do I handle failures or retries for short-scheduled tasks?
- Use `withoutOverlapping()` to prevent overlapping executions and implement retries in your command logic. Log process IDs for debugging and consider exponential backoff for transient failures.
- Is there a way to monitor or log short-scheduled tasks?
- Yes, listen to `ShortScheduledTaskStarting` and `ShortScheduledTaskStarted` events for observability. Use async logging (e.g., Laravel Queues) to avoid blocking the scheduler. Tools like Prometheus can track execution metrics.
- Does this package work with Lumen or other lightweight Laravel frameworks?
- Yes, but you’ll need to manually copy the `ShortScheduleRunCommand` to your project. The package provides explicit instructions for Lumen integration in the [README](https://github.com/spatie/laravel-short-schedule).
- What are the alternatives if I’m on Laravel 11+ or want to avoid this package?
- Laravel 11+ supports sub-minute scheduling natively. For older versions, consider cron-based solutions (e.g., `* * * * * * php artisan command:run`) or queue-based polling. Evaluate trade-offs like precision and resource usage.