在循环中使用 filesize() PHP 函数 - 返回相同的大小

发布于 2024-12-08 11:46:31 字数 1144 浏览 0 评论 0原文

我已经完成了一些 PHP 编码并且熟悉它的各个方面。

我制作了一个 PHP 脚本,它作为 cron 作业运行,将从数据库中提取数据,如果满足某些条件,一些信息就会写入文件中。

由于数据库中可能有多个结果,因此会进行一个循环来运行数据库中的每个结果。

在该循环中,我有另一个循环将数据写入文件。然后使用 cron 作业每分钟调用该文件并运行 bash 脚本中的内容。

因此,PHP 会使用 filesize() 函数循环设置以查看文件中是否写入了任何内容。如果文件大小不为零,则它将休眠 10 秒并尝试再次读取。这是代码:

while(filesize('/home/cron-script.sh') != 0)
{
    sleep(10);
}

不幸的是,当运行文件大小时,它似乎在文件上放置了某种锁或其他东西。 cron 作业可以毫无问题地执行 bash 脚本,脚本中的最后一个命令是将文件清零:

cat /dev/null > /home/cron-script.sh

但是,似乎一旦启动上面的 while 循环,它就会锁定原始文件大小。举个例子,我只是简单地在 cron-script.sh 文件中输入“exit”一词,然后运行一个测试脚本:

while(filesize("/home/cron-script.sh") != 0)
{
    echo "filesize: " . filesize("/home/cron-script.sh");
    sleep(10);
}

循环是无限的,当我输入这个词时,将继续显示“filesize: 4” “出口”。然后,我将在终端发出命令:

cat /dev/null > /home/cron-script.sh 

当我运行上面的测试脚本时,该命令将清除该文件。但是,它仍然显示文件大小为 4 并且永远不会返回到 0 - 因此使 PHP 脚本运行直到达到执行时间限制。

谁能给我一些关于如何解决这个问题的建议?本质上,我只需要某种方法来读取文件大小 - 如果文件中有任何类型的数据,则需要循环执行睡眠例程,直到文件被清除。该文件应在一分钟内清除(因为 cron 作业每分钟调用该 cron-script.sh 文件)。

谢谢你!

I've done a little bit of PHP coding and am familiar with aspects of it.

I have made a PHP script that runs as a cron job that will pull data from a database and if certain conditions are met, some information is written to a file.

Because there may be more than one result in the database, a loop is done to run through each result in the database.

Within that loop, I have another loop which will write data to a file. A cron job is then used to call this file every minute and run the contents in the bash script.

So, the PHP loop it setup to see if the file has anything written to it by using the filesize() function. If the filesize is not zero, then it will sleep for 10 seconds and try to read it again. Here is the code:

while(filesize('/home/cron-script.sh') != 0)
{
    sleep(10);
}

Unfortunately, when the filesize is ran, it seems to place some kind of lock or something on the file. The cron job can execute the bash script without a problem and the very last command in the script is to zero out the file:

cat /dev/null > /home/cron-script.sh

But, it seems that once the while loop above is started, it locks in the original file size. As an example, I just simply put in the word "exit" in the cron-script.sh file and then ran through a test script:

while(filesize("/home/cron-script.sh") != 0)
{
    echo "filesize: " . filesize("/home/cron-script.sh");
    sleep(10);
}

The loop is infinite and will continue to show "filesize: 4" when I put in the word "exit". I will then issue the command at the terminal:

cat /dev/null > /home/cron-script.sh 

Which will then clear the file while I have the test script above running. But, it continues to say the filesize is 4 and never returns to 0 - therefore making the PHP script run until the execution time limit is reached.

Could anyone give me some advice on how I can resolve this issue? In essence, I just need some way to reading the filesize - and if there is any kind of data in the file, it will need to loop through a sleep routine until the file is cleared. The file should clear within one minute (since the cron job calls that cron-script.sh file every minute).

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单挑你×的.吻 2024-12-15 11:46:31

来自 http://www.php.net/manual/en/function.filesize.php

注意:该函数的结果被缓存。有关更多详细信息,请参阅clearstatcache()。

要解决此问题,请记住在调用 filesize() 之前调用 clearstatcache()

while(filesize("/home/cron-script.sh") != 0)
{
    echo "filesize: " . filesize("/home/cron-script.sh");
    sleep(10);
    clearstatcache();
}

From http://www.php.net/manual/en/function.filesize.php

Note: The results of this function are cached. See clearstatcache() for more details.

To resolve this, remember to call clearstatcache() before calling filesize():

while(filesize("/home/cron-script.sh") != 0)
{
    echo "filesize: " . filesize("/home/cron-script.sh");
    sleep(10);
    clearstatcache();
}
我很OK 2024-12-15 11:46:31

filesize 的结果被缓存。

您可以使用 clearstatchace 清除缓存在循环的每次迭代上。

The results of filesize are cached.

You can use clearstatchace to clear the cache on each iteration of the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文