简体   繁体   English

连续运行PHP脚本

[英]Run PHP script continuously

I have a database in MySQL which have entries of time. 我有一个MySQL数据库,有时间条目。 There are more than 1000 entries of time. 有超过1000个时间条目。 I want to extract time and run a PHP script exactly at that time.. 我想在那时提取时间并准确运行PHP脚本..

I have tried to run a PHP script continuously which check the time, but my server does not allow to run the script for more than 60 seconds. 我试图连续运行PHP脚本来检查时间,但我的服务器不允许运行脚本超过60秒。

EDIT. 编辑。 I have to check the database every second. 我必须每秒检查一次数据库。 Is there any alternative? 还有其他选择吗?

Use the pear package, System_Daemon 使用pear包,System_Daemon

http://pear.php.net/package/System_Daemon/ http://pear.php.net/package/System_Daemon/

试试Unix的cron

You'll need some external service to execute the script. 您需要一些外部服务来执行脚本。 On a Unix box, that would be cron . 在Unix机器上,那将是cron On a Windows box, use Task Scheduler . 在Windows框中,使用任务计划程序

Have you thought about writing your process as a server daemon. 您是否考虑过将您的进程编写为服务器守护程序。 It would start up and run in a while loop forever. 它将启动并永远运行一段时间。 Every few minutes or however often you'd like it could check the next x minutes of run times. 每隔几分钟或者经常你会喜欢它可以检查下一个x分钟的运行时间。 You queue up your requests and whenever that time comes around you kick off the script you need to run. 您排队请求,每当这个时间到来时,您就会启动您需要运行的脚本。 I don't think cron is what you'd want since you are trying to schedule future events at arbitrary times... And I'm sure it's what you are currently using to try and check the db every second. 我不认为cron是你想要的,因为你试图在任意时间安排未来事件......而且我确定你正在尝试每秒检查数据库。

You must use the sleep(arg) function that pauses the PHP script for a given time and then continues. 您必须使用sleep(arg)函数暂停PHP脚本一段时间,然后继续。

sleep(50); //Pauses

myFunction(); //Runs after the pause

For example, this pauses the script for 50 seconds. 例如,这会暂停脚本50秒。

编写一个PHP脚本以从数据库中读取并向crontab添加条目,以使脚本在所需的时间运行

Keeping a process running is not a very good solution, not least because you'll need to ensure it does keep running. 保持进程运行不是一个非常好的解决方案,尤其是因为您需要确保它继续运行。 Presumably you know when the next occurrence is going to happen - so use the 'atd' to schedule it - when triggered the script should also work when and how to schedule the next job. 大概你知道下一次发生的时间 - 所以使用'atd'来安排它 - 当被触发时,脚本也应该在何时以及如何安排下一个工作时工作。

This does mean that jobs are chained - and failure of one breaks the chain, also the granularity of most implementations of atd can be rather high. 这确实意味着工作被链接 - 一个人的失败打破了链条,大多数atd实现的粒度也可能相当高。

You might want to look at using a more sophisticated scheduling tool like Nagios or a process monitoring type approach like DJB's daemontools or Oracle's OPMN. 您可能希望使用更复杂的调度工具(如Nagios)或流程监控类型方法(如DJB的daemontools或Oracle的OPMN)。

C. C。

The recommended Pear package - system_daemon looks great. 推荐的Pear包 - system_daemon看起来很棒。 However, the author now recommends to use new functionality that's readily available in Ubuntu: upstart 但是,作者现在建议使用Ubuntu中提供的新功能:upstart

See his article on his own blog from 2012 从2012年开始在他自己的博客上看到他的文章

DJB's DAEMONTOOLS is great. DJB的DAEMONTOOLS很棒。 So, apparently, is systemd and/or Upstart, though Remnant plays a true wanker. 所以,显然,系统和/或暴风雨,虽然Remnant扮演一个真正的wanker。

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

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