How can i use scheduler in seconds manner in Laravel 5.#
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Mastering High-Frequency Scheduling in Laravel: Moving Beyond Standard Cron
Scheduling tasks in Laravel is one of its most powerful features, allowing developers to automate repetitive processes. However, as you discovered, fine-grained scheduling—specifically running a task every 20 seconds—often bumps up against the limitations of the underlying operating system's cron mechanism. As a senior developer, I can tell you that while Laravel provides excellent tools for time-based execution, achieving sub-minute precision often requires shifting the strategy from relying purely on external cron jobs to implementing internal polling logic within your application.
This post will dive into why standard cron scheduling struggles with second-level accuracy and provide robust, practical solutions for handling high-frequency tasks in a Laravel environment.
## The Challenge with Cron and Sub-Minute Intervals
The core issue lies in how system schedulers like Cron operate. They are designed to execute commands based on fixed time intervals (minutes, hours, days). While you can use the pattern `*/20 * * * * *` to run a command every 20 seconds, this approach has significant drawbacks for complex Laravel applications:
1. **Resource Intensive:** Executing an entire PHP process every 20 seconds consumes unnecessary CPU and memory resources, leading to potential system strain if the task isn't extremely lightweight.
2. **Inefficiency:** It forces the application environment to boot up, load frameworks, and execute the scheduler logic repeatedly, which is inefficient for tasks that don't require constant, immediate execution.
3. **Error Handling Complexity:** Managing errors across these rapid executions becomes more complex than dealing with scheduled jobs running hourly or daily.
For most high-frequency needs, relying on external cron calls to trigger a full Laravel Artisan command is overkill and often impractical.
## Solution 1: The Polling Approach (The Recommended Method)
Instead of trying to force the operating system to wake up your application every 20 seconds, a more efficient method in Laravel is to let the scheduled job run on a defined interval (e.g., every minute) and use internal logic to check if the required state has been met or if a specific time window needs processing.
For tasks requiring near real-time updates, you can structure your existing scheduled job to poll for necessary data or process queues frequently.
Here is an example of how you might adapt your scheduler to handle frequent checks:
```php
call(app('TasksController')->handleFrequentChecks)->everyMinute();
// Note: If the task is truly time-sensitive, you handle the logic inside the handler.
}
/**
* Execute the Artisan command.
*
* @return void
*/
public function handle()
{
//
}
}
```
In this setup, the scheduler runs reliably every 60 seconds. The actual logic inside `handleFrequentChecks` can then contain a loop or conditional checks that determine if an action needs to be taken *right now*, based on timestamps stored in your database, effectively simulating sub-minute checking without overloading the system with constant external cron calls.
## Solution 2: Leveraging Queues for Near Real-Time Processing
If the task itself is time-sensitive but doesn't require immediate execution within the web request cycle (e.g., sending an email every 30 seconds), a superior approach is to use Laravel Queues combined with specific queue drivers or specialized workers.
You can schedule jobs on a very tight interval, and then configure your queue worker to process those jobs as fast as possible. While this doesn't guarantee execution *every* 20 seconds from the OS level