cron 时间限制的解决方法
所以基本上我使用 cron 作业来执行需要相当多时间才能运行的 php 脚本(从 ftp 中提取更新的 xml、解压缩、mysql 操作、api 访问等)。问题是我们运行的服务器将 cron 作业的运行时间限制为 15 分钟。
我的问题是:可以使用什么方法来解决这个限制?将脚本分成几部分仍然会导致各部分运行超过 15 分钟的时间限制,所以这是不可能的。有没有办法在执行一段时间后重定向到另一个php脚本?或者是否可以使用 bash 脚本以某种方式在达到 15 分钟的时间限制之前重新启动脚本?
谢谢!
So basically I'm using a cron job to execute a php script that requires quite a bit of time to run (Pulling down updated xml from ftp, unzipping, mysql manipulation, api access, etc.). The issue is that we are running on a server that limits the time a cron job can be run to 15 minutes.
My question is this: what methods can be used to work around this limitation? Breaking the script up into pieces would still causes pieces to run over the 15 minute time limit, so that's out. Are there any ways to redirect to another php script after executing for a certain amount of time? Or could a bash script be used in some way to restart the script before the 15 minute time limit is reached?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的第一个建议是找到一个新的主机,但听起来这不是你现在的选择。
您可以跟踪运行时间,并在达到 15 分钟时间限制之前将您的状态保存在数据库或平面文件中。 cron 中的下一轮将查看数据库或文件,并知道从哪里继续你上次停下的地方。
当然,这取决于您的脚本正在做什么。如果您只是对 X 条记录执行相同的任务,听起来您也许能够实现类似的操作。
My first advice would be to find a new host, but it sounds like thats not an option for you right now.
You could keep track of your runtime and save your state in a DB or flat file just before you hit the 15 minute time limit. The next go-round in cron would then look at the DB or file and know where to pick up where you left off.
This depends on what your script is doing of course. If you're just performing the same task on X number of records, it sounds like you may be able to implement something like this.
有点取决于他们如何实施限制。您也许可以使用 exec() 并在某个时刻启动另一个脚本来中断该进程。
您无法真正“重新启动”脚本。如果可以的话,无论如何,一切都从头开始。
不明白为什么不能分手?您每天可以使用 15 分钟从 cron 启动的脚本还是什么?听起来你需要一个不同的主机提供商,这个限制似乎非常蹩脚。还有15分钟,你在处理什么样的大文件。也许您应该努力让它在 15 分钟内运行。
编辑
我在想,既然限制听起来像是只针对从 cron 运行的脚本,为什么不在另一个系统上拥有实际的 cron,而只使用 wget 来调用远程脚本服务器。
Kinda depends on how they have the limit implemented. You could perhaps use
exec()
and launch another script at some point to break up the process.You can't really 'restart' a script. If you could, it'd just start from scratch anyways.
I don't get why you cant break it up? You get 15 minutes of usage for scripts launched from cron per day or what? Sounds like you need a different host provider, that limit seems very lame. And 15 minutes, what kind of huge files are you dealing with. Maybe you should work on getting it to run in less than 15 minutes.
EDIT
I was thinking, since the limit sounds like it is just on scripts run from cron, why don't you have the actual cron on another system, and just use wget to call the script on the remote server.
您可以尝试让 cron 作业作为脚本后台。就像
我从未尝试过这种策略,因为我从未有过运行那么长时间的 cron 作业,但它会将脚本与 cron 分离
You can try having the cron job background the script. Something like
I have never tried this tactic as I've never had cron jobs that run that long, but it will disassociate the script from cron