使用 CLI 每秒运行一个 PHP 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1726116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a PHP script every second using CLI
提问by Saif Bechan
I have a dedicated server running Cent OS with a Parallel PLESK panel. I need to run a PHP script every second to update my database. These is no alternative way time-wise, it needs to be updated every second.
我有一台运行 Cent OS 的专用服务器,带有 Parallel PLESK 面板。我需要每秒运行一个 PHP 脚本来更新我的数据库。这些在时间上没有替代方法,它需要每秒更新一次。
I can find my script using the URL http://www.somesite.com/phpfile.php?key=123.
我可以使用 URL 找到我的脚本http://www.somesite.com/phpfile.php?key=123。
Can the file be executed locally every second? Like phpfile.php?
文件可以每秒本地执行一次吗?喜欢phpfile.php?
Update:
更新:
It has been a few months since I added this question. I ended up using the following code:
我添加这个问题已经几个月了。我最终使用了以下代码:
#!/user/bin/php
<?php
$start = microtime(true);
set_time_limit(60);
for ($i = 0; $i < 59; ++$i) {
doMyThings();
time_sleep_until($start + $i + 1);
}
?>
My cronjob is set to every minute. I have been running this for some time now in a test environment, and it has worked great. It is really super fast, and I see no increase in CPU nor Memory usage.
我的 cronjob 设置为每分钟。我已经在测试环境中运行了一段时间,并且效果很好。它真的超级快,我看不到 CPU 和内存使用量的增加。
回答by nickf
You could actually do it in PHP. Write one program which will run for 59 seconds, doing your checks every second, and then terminates. Combine this with a cron job which runs thatprocess every minute and hey presto.
你实际上可以在 PHP 中做到这一点。编写一个程序,该程序将运行 59 秒,每秒执行一次检查,然后终止。将此与每分钟运行该过程的 cron 作业结合起来,嘿嘿。
One approach is this:
一种方法是这样的:
set_time_limit(60);
for ($i = 0; $i < 59; ++$i) {
doMyThings();
sleep(1);
}
The only thing you'd probably have to watch out for is the running time of your doMyThings()functions. Even if that's a fraction of a second, then over 60 iterations, that could add up to cause some problems. If you're running PHP >= 5.1 (or >= 5.3 on Windows) then you could use time_sleep_until()
您可能唯一需要注意的是doMyThings()函数的运行时间。即使那是几分之一秒,然后超过 60 次迭代,加起来也可能会导致一些问题。如果您正在运行 PHP >= 5.1(或在 Windows 上 >= 5.3),那么您可以使用time_sleep_until()
$start = microtime(true);
set_time_limit(60);
for ($i = 0; $i < 59; ++$i) {
doMyThings();
time_sleep_until($start + $i + 1);
}
回答by Milan Babu?kov
Have you thought about using "watch"?
你有没有想过使用“手表”?
watch -n 1 /path/to/phpfile.php
Just start it once and it will keep going. This way it is immune to PHP crashing (not that it happens, but you never know). You can even add this inittab to make it completely bullet-proof.
只需启动一次,它就会继续下去。这样它就不会受到 PHP 崩溃的影响(不是它发生了,但你永远不知道)。您甚至可以添加此 inittab 以使其完全防弹。
回答by jW.
Why not run a cron to do this and in the php file loop 60 times which a short sleep. That is the way I have overcome this to run a php script 5 times a minute.
为什么不运行一个 cron 来做到这一点,并在 php 文件中循环 60 次,这是一个短暂的睡眠。这就是我克服这个问题以每分钟运行 5 次 php 脚本的方式。
To set up your file to be run as a script add the path to the your PHP on the first line such as a perl script
要将文件设置为作为脚本运行,请在第一行添加 PHP 的路径,例如 perl 脚本
#!/user/bin/php
<?php
while($i < 60) {
sleep(1);
//do stuff
$i++;
}
?>
回答by Ifnot
This is simple upgraded version of nickfsecond solution witch allow to specify the desired interval in seconds beetween each executions in execution time.
这是nickf第二个解决方案的简单升级版本,允许指定执行时间中每次执行之间的所需间隔(以秒为单位)。
$duration = 60; // Duration of the loop in seconds
$sleep = 5; // Sleep beetween each execution (with stuff execution)
for ($i = 0; $i < floor($duration / $sleep); ++$i) {
$start = microtime(true);
// Do you stuff here
time_sleep_until($start + $sleep);
}
回答by degenerate
I noticed that the OP edited the answer to give his solution. This solution did not work on my box (the path to PHP is incorrect and the PHP syntax is not correct)
我注意到 OP 编辑了答案以给出他的解决方案。此解决方案在我的盒子上不起作用(PHP 的路径不正确且 PHP 语法不正确)
This version worked (save as whatever.sh and chmod +X whatever.sh so it can execute)
这个版本有效(另存为whatever.sh和chmod +Xwhatever.sh,以便它可以执行)
#!/usr/bin/php
<?php
$start = microtime(true);
set_time_limit(60);
for ($i = 0; $i < 59; ++$i) {
echo $i;
time_sleep_until($start + $i + 1);
}
?>

