you cant Write command for that
https://laravel.com/docs/5.6/artisan#writing-commands
and in scheduling every 2 sec run it .
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
We are in the process of developing custom application using laravel. We need to run schedule every 5 sec, but event on that schedule may run 3-4 sec. How to do it? Thanks
you cant Write command for that
https://laravel.com/docs/5.6/artisan#writing-commands
and in scheduling every 2 sec run it .
You probably need to run continuously and in each loop usleep() and pass it the number of microseconds until the next multiple of 5 seconds
Hi Snapey, Thanks for info. Could you give example to run schedule every 5 sec? laravel is providing every ->everyMinute(); (lowest interval). But we want to schedule every 5 sec. Thanks
you cannot do it with schedule since cron will only call the scheduler once per minute
Instead of using the ->everyMinute(); try ->cron('* * * * *'); and specify the exact timing using purely cron syntax. But I am not sure is there a way for triggering in seconds as the smallest value for that syntax is the minute.
Another approach could be creating a 30 tasks with 2seconds delay each. Each will run in every minute but when they have 2 seconds delay then you will trigger the task in every 2 seconds. Hope that makes sense
@m7vm7v yes, thats right. Just remind me, how many minutes are there in 5 seconds? :-p
something like this;
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$dt = Carbon\Carbon::now();
$x=60/5;
do{
// do your function here that takes between 3 and 4 seconds
time_sleep_until($dt->addSeconds(5)->timestamp);
} while($x-- > 0);
})->everyMinute();
}
fire this with your cron job every minute. It will run and take a note of the time, then do your function, return and then sleep until the time it started plus 5 seconds, then repeat until it has done it 12 times and then stops.
The cron job then fires another instance which will do the same.
Things to watch out for. If your function takes more than 5 seconds then you won't manage to call your function 12 times in a minute. This means that cron will start the next batch and you will have two instances running at once. You need to check your own situation to see if that might be an issue for you.
@Snapey as you see the title says 'How to run scheduling every 2 sec'.
lol:
Either way, snapeys way will work fine, just change the 5's to 2's if needed.
Thanks a lot for all the suggestion. We will try to implement and come back.
Lol @ the title and description confusing. But Snapey's suggestion is just fine. Replace the desired secs in there an you're good to go.
Or if it's too confusing for you
protected function schedule(Schedule $schedule) {
$seconds = 5;
$schedule->call(function () use ($seconds) {
$dt = Carbon\Carbon::now();
$x=60/$seconds;
do{
// do your function here that takes between 3 and 4 seconds
time_sleep_until($dt->addSeconds($seconds)->timestamp);
} while($x-- > 0);
})->everyMinute();
}
Whether this won't cause any issue in performance, like if any other service call in between these times in same controller. Whether this work in single thread?
I have tried something like this with no lucky. I am on laravey 9 with php 8.0
$seconds = 5;
$schedule->call(function () use ($seconds, &$schedule) {
$dt = Carbon::now();
$x = 60 / $seconds;
do {
$schedule->command(SomeCommand::class, [...]);
$schedule->command(SomeCommand::class, [...]);
time_sleep_until($dt->addSeconds($seconds)->timestamp);
} while ($x-- > 0);
})->everyMinute();
@XiaoyiWang please make your own thread, and we will help you out
@Sinnbeck Thanks for reply, i will do it.
Please or to participate in this conversation.