PHP 脚本 24/7 运行
是否可以运行 PHP 脚本,以便它可以 24/7 工作,并且在发生崩溃或任何其他情况时只需重新启动?它不应该在浏览器中打开,但可以通过某种方式在服务器端打开,例如通过 cron...它位于 Windows 服务器上。
Is it possible to run a PHP script so it will work 24/7 and in case of crash or anything would just be restarted? It shouldn't be opened in browser, but somehow server-side like by cron... It's on Windows server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
运行两个脚本 - 每个脚本都会检查第二个脚本是否还活着:-) - 并关闭 php.ini 中的时间限制
Run two scripts - each one will check the second one if it is alive :-) - and turn off time limits in php.ini
有一个脚本每
n
分钟运行一次。它应该做的第一件事是检查它是否已经在运行。如果是,则新脚本退出。检查完成后,做一些事情来标记脚本的存在。标准是将您的
pid
写入指定位置。然后检查函数可以:pid
文件是否存在。没有文件,没有运行脚本。pid
是否正在运行。没有正在运行的pid
,没有正在运行的脚本。您有可能会误报正在运行的文件。例如,您的脚本获得的
pid
为 12345。当它终止时,系统已循环遍历所有可能的pid
。现在,另一个进程正在使用pid
12345。pidfile 将包含“12345”;您的脚本将验证具有该编号的进程是否正在运行,查看新进程是否存在,并假设您的脚本正在运行。可能有办法解决最后一个问题,但我不知道。
Have one script that is run every
n
minutes. The first thing it should do is check if it is already running. If so, the new script exits.Once that check is complete, do something to mark the script's existence. The standard is to write your
pid
to a specified location. Then the check function can:pid
file exists. No file, no running script.pid
indicated in the file is running. No runningpid
, no running script.There is a slight chance that you will get a false positive of a running file. For example, your script gets a
pid
of 12345. When it dies, the system has cycled through all its possiblepid
s. Now, another process is usingpid
12345. The pidfile will contain '12345'; your script will verify that a process with that number is running, see that the new process is there, and assume your script is going.There is probably a way around this last problem, but I don't know it.
是的,这是可能的 - 但标准引用计数内存管理器无法处理复杂的对象链接。您可能需要重新编译才能启用循环引用检查垃圾收集器。
更好的解决方案是运行一个快速完成的脚本 - 并使用调度程序频繁运行它(但请考虑如果一个实例在前一个实例完成之前启动,它将如何表现)。
或者,您可能希望在随机的基础上处理所需的处理,由 HTTP 请求触发 - 但在 HTTP 请求的同步部分完成之后(请阅读会话管理中垃圾收集的工作原理)。
了解脚本的用途将会很有帮助。
Yes its possible - but the standard reference counting memory manager won't cope with complex object linking. You may need to recompile to enable the circular reference checking garbage collector.
A better solution would be to run a script which completes quickly - and run it frequently using the scheduler (but do think about how it will behave if an instance starts before the previous one completes).
Alternatively you might want to handle the required processing on a stochastic basis, triggered by an HTTP request - but after the synchronous part of the HTTP request has completed (go read up on how garbage collection works in session management).
It would have been helpful to know what the script is intended to do.
每分钟运行一次脚本并检查最后一个脚本是否仍在运行。
在脚本中更新此文件以防止运行另一个文件
run your script every minutes and check if last one is stil running or not.
update this file inside your script to prevent running another one
在 Windows 服务器上,您不会创建 cron 作业,相当于任务计划程序,可用于自动运行脚本。
就像其他帖子已经说过的那样,创建一个运行时间很短但可以每小时左右调用一次的 php 脚本是最好的方法。如果您确实需要一个真正 24/7 运行的程序,您将需要某种方法将代码编译为二进制文件。最好的选择是 C++ 或 Java。
On a windows server you won't be creating cron jobs, the equivalent is the Task Scheduler which can be used to run the scripts on an automated basis.
Like other posts already stated, creating a php script that would run for a short time, but could be invoked once an hour or so is the best way to go. If you actually need a program to truly run 24/7, you'll need some way to compile your code to binary. Best bet would be C++ or Java.