有没有办法启动 php 脚本并获取状态?
是否可以使用js在网络服务器的后台启动一个php脚本,并让它运行,即使您更改页面或根本不访问该网站,然后如果您在第二个时刻调用php脚本就可以获取当前状态?
这个 php 脚本将处理数据几个小时,并为每个循环休眠 X 秒/分钟。如果我之前问的问题是可能的,如果 php 仅在脚本结束时才生成输出,我怎样才能从中获得“回声”?
也许这不是 PHP 的工作?
谢谢
编辑:在带有 apache 的 Windows 机器上
is it possible to launch a php script in background on the webserver with js and let it run even if you change page or not visit the site at all and then get the current status if you call the php script in a second moment?
This php script will process data for hours and sleep for X seconds/minutes for each loops. If what I asked before is possible how can I even get "echos" from it if php will only generated an output only when the script ends?
Maybe this is not a job for PHP?
thank you
EDIT: on a windows machine with apache
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这当然是可能的 - 我有几个用 PHP 编写的 24/7 运行的脚本。查看在 PHP 中创建守护进程。它提供了有关如何“守护”PHP 脚本以便其像服务一样运行的详细信息,并且还涵盖了信号处理。
要获得调试输出,您可以重定向到日志文件。搜索“unix 重定向输出”,因为有很多可用信息。
It certainly is possible - I have several scripts that run 24/7 written in PHP. Check out Creating Daemons in PHP. It has good info on how to 'daemonize' a php script so that it will run like a service, and it also covers signal handling.
To get debugging output you would redirect to a log file. Do a search on "unix redirect output" as there is a lot of info available.
在 Windows 中,它与 UNIX 没有太大区别。
首先,您需要创建一个带有运行循环的 PHP 脚本。例如,看一下:http://code.google.com/p/php -apns/ 。这是一个 PHP“守护进程”:主脚本 PushMonitor.php 永远运行,因为它有一个无限循环。它定期轮询队列,然后执行操作,然后等待。其实很简单!
在您的情况下,问题是您想从 PHP 脚本启动“守护进程”。
您可能想看看这个: http:// robert.accettura.com/blog/2006/09/14/asynchronous-processing-with-php/ (第一个示例代码)。您将执行类似
launchBackgroundProcess('php myscript.php')
的内容。请注意,代码中有“start /b”命令(对于 UNIX,命令末尾有“&”)。这很重要,因为否则当网页的 PHP 脚本终止时,您的进程将被终止(子进程在父进程死亡后死亡!)。
另外,请记住“php”可执行文件 (cli) 必须位于您的路径中(以便您可以从命令行执行“php”)。
由于启动后台进程的页面的 PHP 脚本即将终止,因此您无法以简单的方式直接捕获“回声”。我的建议是将所有输出写入文件(或数据库等),然后在必要时从该源读取内容。
因此,您将使用 file_put_contents() 等而不是“echo”。
In Windows it's not much different from UNIX.
First of all, you need to create a PHP script with a run loop. For example, take a look at this: http://code.google.com/p/php-apns/ . This is a PHP "daemon": the main script, PushMonitor.php, runs forever, because it has an infinite loop. It polls a queue at regular intervals, then execute the actions and then wait. Really simple, actually!
The problem, in your case, is that you want to launch the "daemon" from a PHP script.
You may want to look at this: http://robert.accettura.com/blog/2006/09/14/asynchronous-processing-with-php/ (first example code) . You will execute something like
launchBackgroundProcess('php myscript.php')
.Note that on the code there's the "start /b" command (and the "&" at the end of the command for UNIX). That is important, because otherwise your process would be killed when the PHP script of the web page is terminated (children process die after parent dies!).
Also, remember that the "php" executable (cli) must be in your path (so you can execute "php" from the command line).
Since the PHP script of the page launching the background process is going to terminate, you can't directly catch the "echoes" in a simple way. My suggestion is to write all output to a file (or a database etc), and then read the contents from that source when necessary.
So, instead of "echo", you will use file_put_contents() etc.