长进程php
我需要运行一个很长的php脚本(四个半小时,五个小时)。
该脚本有时会成功运行,但有时会莫名其妙地被杀死(可能与共享托管有关?)。
我认为运行脚本的解决方案可能是更小的块。
为了做到这一点,我编写了一个脚本来存储它的状态和信息。 xml 文件中的位置,并在移动该位置之前执行脚本的一部分。
我在连接脚本的最后一点时遇到问题,这应该结束当前进程&重新执行脚本。
或者也许我完全找错了树!
我已经通读了我在 SO 和其他地方可以找到的内容,但我仍然不明白:(
请帮忙!!!
丹
i need to run a really long php script (four and half, five hours).
the script sometimes runs successfully, but sometimes gets killed inexplicably (poss something to do with the shared hosting??).
i think that the solution maybe to run the script is smaller chunks.
in order to do this i have written a script that stores it's status & position in an xml file, and executes one chunk of the script, before moving the position on.
i am having problems hooking up the last bit of the script, which should end the current process & re-execute the script.
or maybe i am barking up the wrong tree completely!
i have read through what i can find on SO and elsewhere but i'm still none the wiser :(
please help!!!
dan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
考虑到您有一个永远运行但不会导致数据不一致的脚本,您可以使用 cronjob。
问题是您需要知道脚本是否仍在运行,因为您可能不想启动它两次。我想到的两个解决方案是脚本的进程 ID (getmypid()) 或使用时间戳。
对于 PID:
您需要访问 php 的 exec() (和朋友)和命令行工具,例如 linux 的“ps”。
对于时间戳:
你必须弄清楚X应该是你自己多久。
Considering you have a script that run's forever but won't cause inconsistend data, you could use a cronjob.
The problem is that you need to know if your script is still running, because you likely don't wanna start it twice. Two solutions i have on mind are the process id of the script (getmypid()) or using a timestamp.
For PID:
You need access to php's exec() (and friends) and command line tools like linux' "ps".
For timestamp:
You have to figure out how long X should be yourself.
PHP 不是运行长脚本的首选语言。我假设您在浏览器中运行它?尝试通过 SSH 登录并使用 php /path/to/script.php 运行脚本。
如果这是您可以在本地运行的东西,您可以在本地计算机上安装基本的 Apache / PHP 安装,然后运行它。如果您没有 shell 访问权限,请使用此选项。
PHP isn't the language of choice for running long scripts. I'm assuming you're running this in a browser? Try logging in via SSH and running the script with
php /path/to/script.php
.If this is something you can run locally you could install a basic Apache / PHP install on your local machine and then run it. Use this if you don't have shell access.
如果可能的话,我会尝试将长时间运行的进程拆分为自己的进程。让您的 PHP 脚本启动计算,然后寻找计算可能完成的线索(类似于包含当前计算进度的状态文件)。然后,您甚至可以使用 AJAX 技术为用户提供一个漂亮的小进度条;)
If possible, I would try to split the long running process to be its own process. Make your PHP script launch the calculations and then look for clues that it might be finished (something like a state file that contains the current calculation progress). You could then even use AJAX techniques to give the user a nice little progressbar ;)
如果您有权访问此服务器上的 cron,您可以每分钟运行一次脚本来处理下一个数据块。
If you have access to cron at this server you may run your script every minute to process next chunk of data.
我同意 Josh 的观点,并建议您尽可能以其他方式(例如桌面应用程序)进行编程。 PHP 肯定不是为此类需要脚本运行 4-5 小时的事情而构建的。这肯定会导致 max_execution_time 或脚本耗尽共享托管中的 CPU,但我建议改为使用桌面应用程序或 SSH。
I would agree with Josh and suggest that you program this some other way like a desktop application if possible. PHP was sure not built for something like this which requires the script to run for 4-5 hours. This can be sure to max_execution_time or script eating up the CPU in shared hosting but moving to desktop app or SSH is the workout that I will suggest.