PHP:在第一个脚本运行5分钟后执行脚本

时间:2022-05-19 01:02:17

I'm making a php site, and I would like to have a script (when you click a button) wich adds some info to my mysql database (I can do this part by myself) and it executes a script 5 minutes later. Maybe it's not difficult, but it's hard to google stuff like this, thanks in advance.

我正在制作一个php网站,我希望有一个脚本(当你点击一个按钮时)向我的mysql数据库添加一些信息(我可以自己做这部分)并且它在5分钟后执行一个脚本。也许这并不困难,但很难像这样谷歌的东西,提前谢谢。

10 个解决方案

#1


6  

Sleep is a VERY bad idea. Client browser would have to wait 5 minutes to finish request!!!

睡眠是一个非常糟糕的主意。客户端浏览器必须等待5分钟才能完成请求!

In my opinion it's not possible to do it like you want to.

在我看来,不可能像你想的那样去做。

You should create another script which queries database and checks if there is new data (and on successful fetch does the job). This script should be run by cron every N minutes.

您应该创建另一个查询数据库的脚本,并检查是否有新数据(并且成功获取该作业)。这个脚本应该每隔N分钟由cron运行一次。

#2


5  

Pretty tough one. I'd go for something like this:

相当艰难的一个。我会选择这样的东西:

  1. your original script adds a record to the database, containing its time of execution,
  2. 您的原始脚本会向数据库添加一条记录,包含其执行时间,

  3. another script contains the action that needs to be taken 5 minutes later - but launches it only if the db record mentioned above contains a timestamp of at least 5 minues ago (hope that's clear enough, I'm having trouble phrasing this)
  4. 另一个脚本包含需要在5分钟后执行的操作 - 但只有在上面提到的db记录包含至少5分钟之前的时间戳时才启动它(希望它足够清楚,我在编写这个时遇到了麻烦)

  5. set crontab to execute the second script every X minutes (perhaps 2).
  6. 设置crontab每隔X分钟(可能是2分钟)执行第二个脚本。

It won't be 5 minutes EXACTLY, but rather something between 5 and 7 (in case you choose to launch the script every 2 minutes). Would that do?

它不会是5分钟,而是5到7之间(如果您选择每2分钟启动一次脚本)。那会吗?

#3


4  

You could implement a queue in your database, where you add "commands" to be executed, and also store when to execute this command. Then have a cron job that runs every minute and checks said queue to see if it's time to execute a certain command.

您可以在数据库中实现一个队列,在该队列中添加要执行的“命令”,并存储何时执行此命令。然后有一个每分钟运行一次的cron作业并检查所述队列以查看是否有时间执行某个命令。

#4


3  

If you're on a unix box:

如果你在unix盒子上:

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

Which will schedule the php script.php command to run after 5 minutes.

这将安排php script.php命令在5分钟后运行。

#5


1  

I'm making a browser-based game and I want it to if someone wants to build a building it takes * minutes and then finishes.

我正在制作一个基于浏览器的游戏,如果有人想要建造一个需要*分钟然后完成的建筑,我想要它。

Considering this is your actual goal, I recommend just saving the original building with a timestamp.

考虑到这是您的实际目标,我建议只使用时间戳保存原始建筑物。

I know you tagged your question with PHP, but I don't want to include all the overhead of handling mysql queries in PHP, especially since I don't know how you prefer to execute the queries or what framework you're suing, so here's some pseudocode to handle this "building buildings" task:

我知道你用PHP标记了你的问题,但是我不想包含在PHP中处理mysql查询的所有开销,特别是因为我不知道你更喜欢执行查询或者你正在起诉的框架,所以这里有一些伪代码来处理这个“建筑物”任务:

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!

希望这可以帮助!

#6


0  

IMHO the best way is: On button click save the job to run in the db with the time it should run. Write a small daemon, fetches every 10/5/2 seconds new jobs which should be executed and executes them.

恕我直言最好的方法是:在按钮上单击保存作业以在数据库中运行它应该运行的时间。编写一个小守护进程,每10/5/2秒获取应该执行的新作业并执行它们。

EDIT: Btw the idea using cron for checking for new jobs to execute, is better, but only if you have a small website and you don't need to do load balancing for the jobs.

编辑:使用cron来检查要执行的新作业的想法更好,但只有当你有一个小网站并且你不需要为作业进行负载平衡时。

#7


0  

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

我这样做的方法是在两个脚本之间运行一个cron作业。

  1. the first script sets a value in a database table.
  2. 第一个脚本在数据库表中设置一个值。

  3. the cron job executes the second script. every minute or what not.
  4. cron作业执行第二个脚本。每一分钟或什么不是。

  5. the second script checks for the database value set by script 1 to decide whether to run entirely or not.
  6. 第二个脚本检查脚本1设置的数据库值,以决定是否完全运行。

#8


0  

I would suggest doing the timer in Javascript rather than PHP.

我建议用Javascript而不是PHP来做计时器。

Put a timestamp in the user's $_SESSION to indicate when they started the event, and then have Javascript call back to the browser after five minutes.

在用户的$ _SESSION中添加一个时间戳,以指示他们何时启动该事件,然后在五分钟后将Javascript调用回浏览器。

PHP would still need to know the start time (to prevent the user from hacking the game by tweaking the Javascript time-out), but it wouldn't need to actually do any count-down timing or sleeping or anything like that itself.

PHP仍然需要知道开始时间(以防止用户通过调整Javascript超时来破解游戏),但它不需要实际执行任何倒计时或睡眠或类似的任何东西。

#9


0  

You could fork the process and in the child fork, do a sleep for 5 minutes before executing your second script. I've tested this and it appears the child process will still execute even after the parent has finished. Something like

您可以分叉进程并在子fork中,在执行第二个脚本之前休眠5分钟。我测试了这个,看起来子进程即使在父进程完成后仍然会执行。就像是

//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...

The "return;" is important as the rest of the script will execute a 2nd time (i.e. in the child) unless it's there.

回报;”很重要,因为脚本的其余部分将执行第二次(即在孩子中),除非它在那里。

#10


-3  

Are you looking for that?

你在找那个吗?

sleep php.net

#1


6  

Sleep is a VERY bad idea. Client browser would have to wait 5 minutes to finish request!!!

睡眠是一个非常糟糕的主意。客户端浏览器必须等待5分钟才能完成请求!

In my opinion it's not possible to do it like you want to.

在我看来,不可能像你想的那样去做。

You should create another script which queries database and checks if there is new data (and on successful fetch does the job). This script should be run by cron every N minutes.

您应该创建另一个查询数据库的脚本,并检查是否有新数据(并且成功获取该作业)。这个脚本应该每隔N分钟由cron运行一次。

#2


5  

Pretty tough one. I'd go for something like this:

相当艰难的一个。我会选择这样的东西:

  1. your original script adds a record to the database, containing its time of execution,
  2. 您的原始脚本会向数据库添加一条记录,包含其执行时间,

  3. another script contains the action that needs to be taken 5 minutes later - but launches it only if the db record mentioned above contains a timestamp of at least 5 minues ago (hope that's clear enough, I'm having trouble phrasing this)
  4. 另一个脚本包含需要在5分钟后执行的操作 - 但只有在上面提到的db记录包含至少5分钟之前的时间戳时才启动它(希望它足够清楚,我在编写这个时遇到了麻烦)

  5. set crontab to execute the second script every X minutes (perhaps 2).
  6. 设置crontab每隔X分钟(可能是2分钟)执行第二个脚本。

It won't be 5 minutes EXACTLY, but rather something between 5 and 7 (in case you choose to launch the script every 2 minutes). Would that do?

它不会是5分钟,而是5到7之间(如果您选择每2分钟启动一次脚本)。那会吗?

#3


4  

You could implement a queue in your database, where you add "commands" to be executed, and also store when to execute this command. Then have a cron job that runs every minute and checks said queue to see if it's time to execute a certain command.

您可以在数据库中实现一个队列,在该队列中添加要执行的“命令”,并存储何时执行此命令。然后有一个每分钟运行一次的cron作业并检查所述队列以查看是否有时间执行某个命令。

#4


3  

If you're on a unix box:

如果你在unix盒子上:

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

Which will schedule the php script.php command to run after 5 minutes.

这将安排php script.php命令在5分钟后运行。

#5


1  

I'm making a browser-based game and I want it to if someone wants to build a building it takes * minutes and then finishes.

我正在制作一个基于浏览器的游戏,如果有人想要建造一个需要*分钟然后完成的建筑,我想要它。

Considering this is your actual goal, I recommend just saving the original building with a timestamp.

考虑到这是您的实际目标,我建议只使用时间戳保存原始建筑物。

I know you tagged your question with PHP, but I don't want to include all the overhead of handling mysql queries in PHP, especially since I don't know how you prefer to execute the queries or what framework you're suing, so here's some pseudocode to handle this "building buildings" task:

我知道你用PHP标记了你的问题,但是我不想包含在PHP中处理mysql查询的所有开销,特别是因为我不知道你更喜欢执行查询或者你正在起诉的框架,所以这里有一些伪代码来处理这个“建筑物”任务:

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!

希望这可以帮助!

#6


0  

IMHO the best way is: On button click save the job to run in the db with the time it should run. Write a small daemon, fetches every 10/5/2 seconds new jobs which should be executed and executes them.

恕我直言最好的方法是:在按钮上单击保存作业以在数据库中运行它应该运行的时间。编写一个小守护进程,每10/5/2秒获取应该执行的新作业并执行它们。

EDIT: Btw the idea using cron for checking for new jobs to execute, is better, but only if you have a small website and you don't need to do load balancing for the jobs.

编辑:使用cron来检查要执行的新作业的想法更好,但只有当你有一个小网站并且你不需要为作业进行负载平衡时。

#7


0  

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

我这样做的方法是在两个脚本之间运行一个cron作业。

  1. the first script sets a value in a database table.
  2. 第一个脚本在数据库表中设置一个值。

  3. the cron job executes the second script. every minute or what not.
  4. cron作业执行第二个脚本。每一分钟或什么不是。

  5. the second script checks for the database value set by script 1 to decide whether to run entirely or not.
  6. 第二个脚本检查脚本1设置的数据库值,以决定是否完全运行。

#8


0  

I would suggest doing the timer in Javascript rather than PHP.

我建议用Javascript而不是PHP来做计时器。

Put a timestamp in the user's $_SESSION to indicate when they started the event, and then have Javascript call back to the browser after five minutes.

在用户的$ _SESSION中添加一个时间戳,以指示他们何时启动该事件,然后在五分钟后将Javascript调用回浏览器。

PHP would still need to know the start time (to prevent the user from hacking the game by tweaking the Javascript time-out), but it wouldn't need to actually do any count-down timing or sleeping or anything like that itself.

PHP仍然需要知道开始时间(以防止用户通过调整Javascript超时来破解游戏),但它不需要实际执行任何倒计时或睡眠或类似的任何东西。

#9


0  

You could fork the process and in the child fork, do a sleep for 5 minutes before executing your second script. I've tested this and it appears the child process will still execute even after the parent has finished. Something like

您可以分叉进程并在子fork中,在执行第二个脚本之前休眠5分钟。我测试了这个,看起来子进程即使在父进程完成后仍然会执行。就像是

//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...

The "return;" is important as the rest of the script will execute a 2nd time (i.e. in the child) unless it's there.

回报;”很重要,因为脚本的其余部分将执行第二次(即在孩子中),除非它在那里。

#10


-3  

Are you looking for that?

你在找那个吗?

sleep php.net