PHP: execute a script 5 minutes after starting the first script

I am making a php site and I would like to have a script (when you click the button) that adds some information to my mysql database (I can do this part separately) and it runs the script 5 minutes later. It may not be difficult, but it is difficult for Google like this, thanks in advance.

+8
php events mysql
source share
10 answers

Sleep is a VERY bad idea. To complete the request, the browser browser will have to wait 5 minutes.

In my opinion, it is impossible to do as you want.

You need to create another script that queries the database and checks to see if there is new data (and if the selection succeeds, it does the job). This script should be run cron every N minutes.

+6
source share

Pretty tough. I would go for something like this:

  • your original script adds a record to the database containing its execution time,
  • another script contains an action that needs to be taken after 5 minutes, but runs it only if the db entry mentioned above contains a timestamp at least 5 minutes ago (I hope it's clear enough, I had a problem with the phrase this)
  • set crontab to execute a second script every X minutes (maybe 2).

It will not be 5 minutes EXACTLY, but rather something between 5 and 7 (if you decide to run the script every 2 minutes). This will?

+5
source share

You can implement the queue in your database, where you add โ€œcommandsโ€ to execute, and also save when to execute this command. Then run the cron job, which runs every minute, and checks the specified queue to see if it is time to execute a specific command.

+4
source share

If you are in a unix window:

exec("echo 'PHP .php' | at now +5 minutes"); 

Schedules the launch of the PHP .php in 5 minutes.

+3
source share

I am making a browser game, and I want someone to want to build a building, in minutes, and then it ends.

Given that this is your actual goal, I recommend just keeping the original building with a time stamp.

I know that you tagged your question using PHP, but I donโ€™t want to include all the overhead of processing mysql queries in PHP, especially since I donโ€™t know how you prefer to execute queries or what structure you use suing, so here is some pseudo-code for solving this "building" problem:

build.php

 building_type_id = sanitize(POST['id']) user_id = current_user['id'] query('INSERT INTO buildings (user_id, building_type_id, created_at) VALUES (' + user_id + ', ' + building_type_id + ', CURRENT_TIME)'); 

my_buildings.php

 user_id = current_user['id'] completed_buildings = query('SELECT * FROM buildings b LEFT OUTER JOIN building_types t ON b.building_type_id = t.id WHERE DATE_ADD(b.created_at, INTERVAL t.construction_time SECOND) < NOW();') under_construction = query('SELECT * FROM buildings b LEFT OUTER JOIN building_types t ON b.building_type_id = t.id WHERE DATE_ADD(b.created_at, INTERVAL t.construction_time SECOND) > NOW();') 

Hope this helps!

+1
source share

IMHO in the best way: Click the "Save" button to save the task that will be executed in db from the moment it starts. Write a small daemon, select every 10/5/2 seconds new tasks to be performed and run.

EDIT: It's better to use the cron idea to test new jobs, but only if you have a small website and you don't need to perform load balancing for the jobs.

0
source share

The way I will do this is to run a cron job between two scripts.

  • The first script sets the value in the database table.
  • cron job executes the second script. every minute and what not.
  • the second script checks the database value given by script 1 to decide whether to fully run or not.
0
source share

You can branch the process in the child fork, sleep for 5 minutes before executing the second script. I tested this, and it seems that the child process will run even after the parent process has completed. Something like

 //initial code $pid = pcntl_fork(); //fork the process if ($pid==0) // if in the child { exec("sleep 300; php second_process.php"); //sleep for 5 minutes and execute second script return; // or exit } // rest of initial script... 

"return;" important since the rest of the script will execute a second time (i.e. in the child) if it is not there.

0
source share

I would suggest doing a timer in Javascript, not PHP.

Put a timestamp in your custom $ _SESSION to indicate when it triggered the event, and then redirect Javascript to the browser after five minutes.

PHP still needs to know the start time (so that the user cannot hack the game by setting the Javascript timeout), but in fact it would not be necessary to take any readings or sleep or something like that on its own.

0
source share

Are you looking for this?

sleep php.net

-3
source share

Source: https://habr.com/ru/post/651406/


All Articles