Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mansa06's avatar

How to run scheduling every 2 sec

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

0 likes
18 replies
Snapey's avatar

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

1 like
mansa06's avatar

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

Snapey's avatar

you cannot do it with schedule since cron will only call the scheduler once per minute

m7vm7v's avatar

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

Snapey's avatar

@m7vm7v yes, thats right. Just remind me, how many minutes are there in 5 seconds? :-p

Snapey's avatar

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.

3 likes
m7vm7v's avatar

@Snapey as you see the title says 'How to run scheduling every 2 sec'.

lostdreamer_nl's avatar

lol:

  • Title: How to run scheduling every 2 sec
  • Description: We need to run schedule every 5 sec
  • First comment: Could you give example to run schedule every 5 sec? [...] But we want to schedule every 5 sec.

Either way, snapeys way will work fine, just change the 5's to 2's if needed.

1 like
mansa06's avatar

Thanks a lot for all the suggestion. We will try to implement and come back.

LaptopGuy's avatar

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.

Cronix's avatar

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();
}
2 likes
rvkvino's avatar

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?

XiaoyiWang's avatar

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();

Please or to participate in this conversation.