简体   繁体   English

PHP脚本似乎执行了两次

[英]PHP scripts seem to be executing twice

I have a generic PHP maintenance script that sets a database value to prevent running at the same time as another instance. 我有一个通用的PHP维护脚本,该脚本设置一个数据库值以防止与另一个实例同时运行。 Usually run via cronjob. 通常通过cronjob运行。

I cleared the database value and tried running this script manually with the cronjob turned off. 我清除了数据库值,并尝试在cronjob关闭的情况下手动运行此脚本。 Every time I run it from the browser, it terminates immediately stating it is already running. 每次我从浏览器运行它时,它都会立即终止,说明它已经在运行。

The script will run for about 30 seconds as a background process then terminate automatically as if PHP detected the browser was closed (should take about 15 min to complete). 该脚本将在后台运行约30秒钟,然后自动终止,就像PHP检测到浏览器已关闭一样(大约需要15分钟才能完成)。

So I added code to echo when the database value is set or read. 因此,我添加了代码以在设置或读取数据库值时回显。 It never echos when it's set, only when it was read, but I can see the database value is stored each time. 它永远不会在设置时回显,只有在读取时才会回显,但是我可以看到每次都存储数据库值。

Script always finishes as expected if run from cron. 如果从cron运行,脚本总是会按预期完成。

What could be going on? 可能会发生什么? Could the server be executing scripts twice on each browser based request? 服务器能否在每个基于浏览器的请求上执行两次脚本?

Server runs Hive so different dirs can have different PHP versions. 服务器运行Hive,因此不同的目录可以具有不同的PHP版本。 Don't know if this could have something to do with it. 不知道这是否与它有关。

PHP 5.2.17 (default)
PHP 5.3.27 (dir this script is in)
Apache 2.2.25

Code that dictates if it runs is simply this: 指示其是否运行的代码就是这样:

$DB = new DbConnector($db_name, $db_user, $db_pass);

if ($DB->queryOne("SELECT COUNT(*) FROM data_vars WHERE name = 'maintenance_running'")) {
    exit('Already running!');
} else {
    $DB->query("INSERT INTO data_vars (name, value) VALUES ('maintenance_running', 1)");
}

At the end of the script, the value is cleared. 在脚本末尾,将清除该值。 Again, this problem only happens when run from the browser. 同样,仅当从浏览器运行时,才会发生此问题。

You should set a longer value for execution limit with: 您应该使用以下命令为执行限制设置一个更长的值:

set_time_limit(30*60) // 30 minutes

Also the echo function probably is working, but as the response is sent to the browser until the whole script ends executing that is the cause that you can't see the print, because it´s killed by the execution limit before it ends and prints the response. 另外,echo函数可能还在起作用,但是由于响应被发送到浏览器,直到整个脚本执行完毕,这是您看不到打印内容的原因,因为它在结束并打印之前被执行限制杀死了响应。

You can try echoing partially the response with the help of the 您可以尝试借助

ob_start

and

ob_flush

You can read more about ob_functions here 您可以在此处阅读有关ob_functions的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM