简体   繁体   English

顺序和连续执行PHP脚本

[英]Execute php script Sequentially and Continuously

I have developed a script which should execute continuously and sequentially. 我开发了一个应该连续执行的脚本。 Now on creating a cron job for this script, it keeps executing asynchronously. 现在,在为此脚本创建一个cron作业时,它将继续异步执行。

1) I kept a while loop in script and thought of executing this script once so i used @reboot to execute the script once but if apache craches then this script will not start executing by its own ?? 1)我在脚本中保持了一个while循环并想到执行这个脚本一次所以我使用@reboot执行一次脚本但是如果apache崩溃那么这个脚本将不会自己开始执行?

2)Using * * * * * cron it executes this script but creates multi-thread and all cron overlaps on the server eventually. 2)使用* * * * * cron执行此脚本但最终在服务器上创建多线程和所有cron重叠。

I have ran out of ideas how to execute a server script continuously and sequentially even if apache server restarts. 即使apache服务器重新启动,我已经没有想法如何连续和顺序地执行服务器脚本。

You're asking for: 你要求:

a script which should execute continuously and sequentially 一个应该连续和顺序执行的脚本

That's the definition of a daemon. 这是守护进程的定义。 You can use upstart to easily create a daemon with your php code. 您可以使用upstart轻松地使用您的PHP代码创建一个守护进程。

Here you can find a good article to explain how create a daemon with upstart and node.js (but is easy to adapt to a php script): http://kvz.io/blog/2009/12/15/run-nodejs-as-a-service-on-ubuntu-karmic/ 在这里你可以找到一篇很好的文章来解释如何使用upstart和node.js创建一个守护进程(但很容易适应php脚本): http//kvz.io/blog/2009/12/15/run-nodejs -as一种服务上,Ubuntu的业力/

cron is for repeating jobs on a timed basis (min interval: 1 minute). cron用于定时重复工作(最小间隔:1分钟)。 It's intended for scripts that eventually exit and need to be restarted from scratch. 它适用于最终退出并需要从头开始重新启动的脚本。 Sounds like your script is essentially trying to be a daemon - started once, then left running permanently. 听起来你的脚本本质上是试图成为一个守护进程 - 启动一次,然后永久运行。

Instead of cron to repeatedly start new copies of the script, simply start your script ONCE via an init script, eg /etc/rc.local 而不是cron重复启动脚本的新副本,只需通过init脚本启动脚本ONCE,例如/etc/rc.local

有一些想法可以处理检查脚本是否在此问题中运行,并且您可以每隔几分钟运行一次cron以确保其运行,如果没有则启动它。

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

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