长进程php

发布于 2024-08-29 13:48:25 字数 311 浏览 3 评论 0原文

我需要运行一个很长的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 技术交流群。

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

发布评论

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

评论(5

溇涏 2024-09-05 13:48:25

考虑到您有一个永远运行但不会导致数据不一致的脚本,您可以使用 cronjob。

问题是您需要知道脚本是否仍在运行,因为您可能不想启动它两次。我想到的两个解决方案是脚本的进程 ID (getmypid()) 或使用时间戳。

对于 PID:

  • 在脚本启动时保存 PID(到 /tmp/script_pid)
  • 每分钟触发 cronjob。查找活动进程并保存PID,如果没有找到则启动。

您需要访问 php 的 exec() (和朋友)和命令行工具,例如 linux 的“ps”。

对于时间戳:

  • 每次迭代保存时间戳。
  • 每分钟触发一次 cronjob。如果时间戳早于 X,则启动新进程。

你必须弄清楚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:

  • Save PID on script startup (to /tmp/script_pid)
  • Trigger cronjob each minute. Lookup active process with save PID, and start if not found.

You need access to php's exec() (and friends) and command line tools like linux' "ps".

For timestamp:

  • Save timestamp each iteration.
  • Trigger cronjob each minute. If timestamp is older than X, start new process.

You have to figure out how long X should be yourself.

一口甜 2024-09-05 13:48:25

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.

我还不会笑 2024-09-05 13:48:25

如果可能的话,我会尝试将长时间运行的进程拆分为自己的进程。让您的 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 ;)

白昼 2024-09-05 13:48:25

如果您有权访问此服务器上的 cron,您可以每分钟运行一次脚本来处理下一个数据块。

If you have access to cron at this server you may run your script every minute to process next chunk of data.

眼藏柔 2024-09-05 13:48:25

我同意 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.

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