简体   繁体   English

PHP:如何确保我的脚本可以长时间运行?

[英]PHP : How can I ensure my script is long running?

I am developing a simple application which will allow me to migrate emails from one IMAP server to another. 我正在开发一个简单的应用程序,它将允许我将电子邮件从一台IMAP服务器迁移到另一台IMAP服务器。

Although the script works - it may be the case that some IMAP Mailboxes are very large, and the script is running for a long time. 尽管脚本可以正常工作-某些IMAP邮箱可能非常大,并且脚本运行了很长时间。 I know that I can set the time limit of my script to never end using : set_time_limit(0) 我知道我可以将脚本的时间限制设置为永不结束: set_time_limit(0)

However ideally I would like to be able to set it to start and be able to view the webpage later and see if it is still running or finished. 但是,理想情况下,我希望能够将其设置为启动,以后再查看该网页,看看它是否仍在运行或完成。 Is there a way of starting a process and then coming back to view that same process? 有没有一种方法可以启动一个过程,然后再返回查看同一过程?

I'm not really sure of the right terminology to when asking a question like this, so I hope I have made myself clear :) 当我问这样的问题时,我不太确定要使用的术语是正确的,所以我希望我已经清楚了:)

EDIT: I've just seen the function ignore_user_abort() but I'm not sure how this will allow me to come back to the same page and see if the process is still running, stopped due to an error, or whether it has finished. 编辑:我刚刚看过函数ignore_user_abort()但是我不确定这将如何让我回到同一页面,查看进程是否仍在运行,是否由于错误而停止,或者是否已经完成。

Setup a cron job to run your script and have it log it's status to a text log somewhere. 设置一个cron作业以运行您的脚本,并将其状态记录到某处的文本日志中。 Then all you have to do is check your logs. 然后,您要做的就是检查您的日志。

In a normal HTTP request, PHP is run as a sub process to some other program. 在正常的HTTP请求中,PHP作为其他程序的子进程运行。 Typically Apache, but it's all a matter of what your web server is. 通常是Apache,但这完全取决于您的Web服务器。 The problem is that, even with the options in PHP there's always a chance the web server will end the PHP process for a variety of different reasons. 问题是,即使使用PHP中的选项,Web服务器也总是有机会出于各种不同的原因而终止PHP进程。

The best solution to having a long running PHP script is to run the script through the Command Line Interface (CLI) http://php.net/manual/en/features.commandline.php 拥有长期运行的PHP脚本的最佳解决方案是通过命令行界面(CLI) http://php.net/manual/zh-cn/features.commandline.php运行脚本

There it can run for as long as it wants. 它在那里可以运行任意长时间。 I would still use the set_time_limit(0) but check the link above for the specifics. 我仍将使用set_time_limit(0)但请检查上面的链接以获取详细信息。

Also, if you don't have access to the command line on your server, typically you'll have access to some sort of task scheduler (CRON in Linux). 另外,如果您无权访问服务器上的命令行,则通常可以访问某种任务计划程序(Linux中为CRON)。 These typically work by executing a command as if it were on the command line, so you could setup a task to run the PHP script on demand. 这些通常通过执行命令来工作,就像在命令行上一样,因此您可以设置任务以按需运行PHP脚本。

As for the status updates. 至于状态更新。 You can have the migration code write the status to a database, or text file. 您可以让迁移代码将状态写入数据库或文本文件。 Then you create a second, normal PHP page that displays the status updates. 然后,创建第二个正常的PHP页面,以显示状态更新。

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

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