我有一个 PHP 脚本已经运行了一周,因为它正在做大量的数字运算——它没有挂起,只是做了很多工作。问题是我不知道它去了哪里,所以我不知道它是否会在 1 小时或 1 个月内完成。(是的,我应该在里面放点东西让它告诉我,但现在太晚了。)
有什么办法可以找出脚本在做什么?或者更好的是,从当前状态中提取变量?
我有一个 PHP 脚本已经运行了一周,因为它正在做大量的数字运算——它没有挂起,只是做了很多工作。问题是我不知道它去了哪里,所以我不知道它是否会在 1 小时或 1 个月内完成。(是的,我应该在里面放点东西让它告诉我,但现在太晚了。)
有什么办法可以找出脚本在做什么?或者更好的是,从当前状态中提取变量?
在 Windows 上,您可以使用 Microsoft 的procmon.exe检查 php.exe 正在做什么。它不会为您提供有关变量等的完整反馈,但您可以检查任何文件系统操作(php 经常这样做)。PHP 有一些内部函数存储为额外的 .exe 文件。如果 PHP 调用它们,您可以使用 procmon 检查...
我希望脚本现在已经完成了它的任务。作为一般指南,在 Linux 上,您可以使用 top 命令检查系统上运行的进程发生了什么。它为您提供了所有正在运行的进程的窗口。
要专注于单个进程,您可以通过 ps 命令获取其 PID(进程 ID)。所以你可以运行:
$ ps -e
此命令将提供 4 列输出。左列是 PID,而右列是脚本名称。假设您在右栏中找到脚本名称,并检查其 PID 是否为 3468。然后运行
$顶部-p 3468
你会得到定期更新的顶部窗口。您可以在其中选择不同类型的信息。通常你首先检查 %CPU 和 VIRT。VIRT 列很有用,因为如果它不断增加,这几乎是脚本中内存泄漏的明确迹象。
或者,您可以使用 htop 并突出显示您选择的进程,如果您在系统上安装了它。
The already suggested answers.. Simply show that a script is running.. This as you already stated is not your need...
As for a script already running that does not log / store any information.. Well the only true option you have that i can think of is checking what it is processing.. So if it is processing files / a database.. You should be able to see the changed / modified results.
If its purely all in memory, your more or less out of luck unless you happen to use something like a session.
But simply checking if the script is running... is of no help at all.., So if its processing a database, check the database, If its processing files, check the files.. Otherwise your out of luck.
When it comes to heavy cruncher scripts, for example when i do geo-coding... I store results / processed data in local files.. So if the script fails or stops.. I can resume.. But the benefit of this is i can run another script and read those files and see the progress or last step performed.
CAN this be resumed or would it start over again? If it can be resumed, stop it and have it log or something so you can monitor it? Otherwise your just going to have to wait for it to finish.
I have written some PHP scripts that take a very long time to run. Usually I have the script update a database table with some statistics occasionally (ex: if in a loop, every 10000 loops, depending on weight of the loop...also take samples of microtime to see how long a particular step takes). I can then check the table for the "statistics" to see how the script is performing, or what particular step it is on.
I'd agree with what others have said about PHP not being the best language for this, but in a pinch I've found this method works well (I've had PHP scripts run stable for months).