Laravel schedular: execute a command every second

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Beyond Minutes: Executing Tasks Every Second in Laravel Scheduling As a senior developer, I often encounter scenarios where business logic demands real-time execution—tasks that need to run continuously, such as sending notifications based on device status updates. Your requirement to execute a function every second for continuous WebSocket notifications highlights a common architectural challenge when mixing the convenience of scheduled tasks (like Laravel’s Scheduler) with the demands of high-frequency, real-time data processing. While Laravel’s Scheduler is an incredibly powerful tool for managing periodic maintenance and batch jobs, it operates fundamentally on a cron-like system, which typically executes tasks at minute or hour intervals. Trying to force a loop running every single second *inside* the scheduler mechanism is generally inefficient, resource-intensive, and architecturally unsound for continuous monitoring. This post will explore why the standard approach falls short and present a superior, developer-focused alternative for handling high-frequency, real-time data streams in a Laravel application. ## The Pitfalls of Sub-Second Scheduling When you use `$schedule->call(function(){ ... })->everySecond();`, you are essentially asking the underlying system to wake up your PHP process every second, execute the code, and then go back to sleep. For high-frequency tasks: 1. **Overhead:** The constant waking, booting, and executing of the application context introduces significant CPU and memory overhead that is unnecessary for a simple status check. 2. **Inefficiency:** Polling every second (even if successful) is less efficient than an event-driven model. You are constantly asking the device, "Is there anything new?" instead of waiting for the device to tell you when something *has* happened. 3. **Cron Limitations:** Standard cron systems and Laravel’s scheduler are designed for batch processing, not micro-second execution. They are not optimized for this level of granularity. If your goal is continuous notification delivery based on a device status, the most robust solution shifts the paradigm from **polling** to **event-driven communication**. ## The Recommended Architecture: Event-Driven Real-Time Communication For continuous updates like WebSocket notifications, the ideal pattern involves establishing a persistent connection rather than relying on repeated server-side polling. This approach leverages technologies designed for real-time data flow, which is often more scalable and performant than constant HTTP requests or scheduled jobs. Here is how you can structure your system effectively: ### 1. Device Communication via WebSockets Instead of having the Laravel scheduler constantly poll a device, the device itself should push status updates to your application immediately when a change occurs. This is the core principle behind technologies like WebSockets (or services built on top of them, such as Pusher or Laravel Echo). * **Device Side:** The device establishes a persistent connection and pushes status changes directly to a message broker or WebSocket server. * **Laravel Side:** Your application listens for these incoming events. When an event arrives (e.g., "Status changed to 'Connected'"), the application immediately triggers the notification logic without waiting for a scheduled cron job to run. ### 2. Using Laravel Queues for Periodic Checks (If Polling is Necessary) If, for some reason, you absolutely must poll the device every minute or few minutes (not every second), the correct way to handle this in Laravel is by using **Queues**. This offloads the heavy work from the HTTP request cycle and allows the job to be handled asynchronously. You would define a job that performs the status check: ```php // app/Jobs/CheckDeviceStatus.php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class CheckDeviceStatus implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable; protected $deviceId; public function __construct($deviceId) { $this->deviceId = $deviceId; } /** * Execute the job. */ public function handle() { // Connect to the device and process its response (This runs asynchronously) $status = $this->fetchStatusFromDevice($this->deviceId); if ($status) { // Trigger notification logic here, perhaps pushing to a WebSocket channel Notification::route('websocket', 'device_updates')->notify( new DeviceStatusUpdate($this->deviceId, $status) ); } } } ``` You would then schedule this job using your standard scheduler: ```php // app/Console/Kernel.php protected function schedule(Schedule $schedule): void { // Run the status check every minute (or whatever frequency is appropriate) $schedule->job(new CheckDeviceStatus(123)) // Pass necessary parameters ->everyMinute(); } ``` ## Conclusion The core takeaway is that for real-time systems, avoid forcing continuous polling through scheduled tasks. The Laravel Scheduler excels at batch processing and maintenance. For continuous notifications based on external device status, embrace modern, event-driven architectures using **WebSockets** for persistent communication. If periodic checks are required, leverage **Laravel Queues** to ensure these operations are handled asynchronously and efficiently, leading to a more stable, scalable application. Exploring the principles behind robust Laravel development, as championed by resources like [laravelcompany.com](https://laravelcompany.com), will guide you toward building systems that are not just functional, but truly performant.