开发始终运行PHP脚本 [英] Developing always running PHP script
问题描述
(我们的服务器是基于Linux的)
我是一个经验丰富的PHP开发人员,但我第一次开发一个机器人,它总是运行并获取一些数据。 / p>
我将用一个简单的(和示例)方案解释我的应用程序。我有大约2000网站url和我的应用程序将访问此网址和记录网页的内容。这个应用程序将工作7天24小时。它将开始工作,当它完成2000网站。
但我需要一些建议我的服务器。如您所见,我的应用程序将运行 infinity ,直到我关闭服务器。我可以这样做这个无限循环:
while(true)
{
应用程序代码这里
}
但我认为这将是一个邪恶的服务器:)在服务器端可以做这样的事情吗?
我想使用 cronjobs ,但它不适用于我的场景。因为我的脚本开始工作,asap它完成工作。我必须完成工作后重新开始,而不是每30分钟开始。因为我不知道,也许获取所有2000个网站,将需要超过30分钟或不到30分钟。
我希望我解释得很好。 p>
我也担心内存使用。因为你知道垃圾收集器清除每个PHP脚本停止后的内存。但正如我所说,我的应用程序不会停止几天(也许几个星期)。所以垃圾回收器不会被触发。我手动取消设置( unset()函数)结束时所有使用的变量脚本。是否足够?
我需要服务器管理员的一些建议:)
strong>我将它开发为控制台应用程序,而不是Web应用程序。我可以从命令行执行它。
您的脚本可以只运行一次列表并退出。这样,可以释放资源php持有的资源。
然后有一个shell脚本调用无限循环中的php脚本。
由于php不是为长时间运行的任务而设计的,我不确定垃圾收集是否由任务决定。每次运行后退出将会强制它释放一切。
(Our server is Linux based)
I'm an experienced PHP developer but first time i'll develop a bot which always running and fetch some datas.
I'll explain my application with a simple (and sample) scenario. I have about 2000 web site url and my application will visit this url's and record contents of web page's . This application will work 7 days 24 hours. It will start working again when it's finish 2000 web sites.
But i need some suggestions for my server. As you see, my application will be run infinity until i shut down server. I can do this infinity loop with this :
while(true)
{
APPLICATION CODES HERE
}
But i think this will be an evil for server :) Is it possible to doing something like this, on server side?
Also i think using cronjobs but it's not work for my scenario. Because my script start working again asap it's finish working. I have to "start again when you finish your work" , not "start every 30 minutes" . Because i don't know, maybe fetching all 2000 websites, will take more than 30 minutes or less than 30 minutes.
I hope i explained it very well.
Also i'm worried about memory usage. As you know garbage collector cleans memory after every PHP script stop. But as i said, my app won't stop for days (maybe weeks) . So garbage collector won't be triggered. I'm manually unsetting (unset() function) all used variables at end of script. Is it enough?
I need some suggestions from server administrators :)
PS. I'm developing it as console application, not a web application. I can execute it from command line.
Your script could just run through the list once and quit. That way, what ever resources php is holding can be freed.
Then have a shell script that calls the php script in an infinite loop.
As php is not designed for long running task, I am not sure if the garbage collection is up to the task. Quiting after every run will force it to release everything.
这篇关于开发始终运行PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!