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

mindhunter's avatar

How to run a Curl script file at specific time just once in Laravel?

Hi everybody. I need a solution for my problem here. I have a website in which user can set some task to run at specific time. The tasks are about calling (POST) some API at specified time. For that i create a PHP Curl Script File for each Task and now want to run the Script at their time period. I googled around and found that there is some Cron job and Scheduler in Laravel but they are run forever at specified date and time. The thing i need is to Call script File at Time A and after TIme A my task should be terminated and do not run ever. I appreciate if any pros give me solution.

0 likes
8 replies
mindhunter's avatar

thanks. I met this answer a lot when google about it but cron is repeat the job at that time and also do not support second. I want to start the job for example at 2020-08-30 12:20:20 second and never repeat that again.

mindhunter's avatar

Thanks for your answer. Maybe i demonstrate my question in bad way. I just want to run my job once and do not want to repeat it. I want to start the job (execute my php curl script) for example at 2020-08-30 12:20:20 and never repeat that again. As i understand this library repeat the job. That's not i am looking for.

Snapey's avatar

have a jobs table

put the job in the table, with the exact time it should run

Have a scheduled task that checks the table once per minute. It collects any jobs that should be run in that minute.

If it finds any, it sleeps for the number of seconds remaining until the correct time.

The CURL task is then performed and the job removed from the database.

1 like
mindhunter's avatar

@snapey Thank u. You always have Engineered answer for me over years. i have a table that collects user tasks. It has a column with the time that related task should be run. for Example it is 2020-08-30 12:20:20. So I think i should Have a scheduled task that checks the table once per Second. The table is not that big so the query time is not so much trouble. I did not buy your sentence "If it finds any, it sleeps for the number of seconds remaining until the correct time". Could you walk me through it?

Snapey's avatar
Snapey
Best Answer
Level 122
// find jobs that should start in the next minute
$jobs = Job::whereBetween('runtime',[now(),now()+addMinutes(1)])->orderBy('runtime')->get();

if(!$jobs) {
    return;        //nothing to do
}

foreach($jobs as $job) {

	sleep($job->runtime-now());	// sleep for the difference between now and the runtime

	// continue and perform curl task

}

There are some problems with this however. If more than one job can run per minute then you may be executing one at the time that a second should be executed. I don't know if you can guard for this. I mitigated this slightly by ordering the jobs in time order, however the Curl could take quite a few seconds to execute.

1 like
mindhunter's avatar

I am just exec a PHP curl file (which is out of Laravel Scope) per task and i think the job does not have to wait for the curl operation So that would be OK. Let me write my code and be in touch before closing the thread.

Please or to participate in this conversation.