PHP 项目中的简单 cron 作业管理应该使用什么?
我想要在 PHP 项目中进行简单的类似 cron 的管理 有一些我想要的东西:
- php job worker 只是简单的脚本,放置在项目目录内的某个子目录中,
- 项目根目录中有像 /cron/daily、/cron/monthly ... 等子树,其中包含所有内容工作人员
- 没有必要在添加每个工作人员时搞乱 crontab。
- 所有脚本都由类似 run-parts 的东西以相应的频率运行,并且它们各自的输出被记录到单独的文件中,例如 /var/log/projectname/cron/daily/somescript.log
- 如果 /cron/daemon 目录包含应该永远(每分钟)运行但不超过 1 个实例的脚本
我在一个项目中体验过这种调度系统,并且喜欢它。它提供了许多巧妙的东西:
- 作业是项目脚本,驻留在项目目录中,由 git 跟踪。
- 不需要搞乱 crontab。
- 日志已整理。
- 守护进程很容易构建。
我只会在项目 /cron 子目录上使用 /bin/run-parts ,但无法按照我想要的方式分割日志。分割日志记录是一个非常好的功能。
所以,我只是想这种系统以前已经创建过很多次了,有没有现成的PHP项目解决方案?基本上它只是一些更智能的运行部件的等效物。是不是应该再写一遍?
PS 还有很多作业队列特定的解决方案,例如 Gearman。他们很棒,但这个问题是关于项目 cron jobs 轻量级解决方案。
I want a simple cron-like management in PHP project
there are some things I would like to have:
- php job worker is just plain script that is placed in some subdir inside project directory
- there is subtree like /cron/daily, /cron/monthly ... etc in the project root that contains all that workers
- there is no need to mess with crontab with every worker added.
- all scripts are run by something like run-parts with the corresponding frequency, and their respective output is logged into separate files like /var/log/projectname/cron/daily/somescript.log
- would be great to have /cron/daemon dir containing scripts that should be run forever (minutely) but no more than 1 instance
I've had experience with that kind of scheduling system in one project and loved it. It provides a number of neat things:
- jobs are project scripts and reside in project dir, tracked by git.
- no need for crontab messing.
- logs are sorted out.
- daemons are easy to build.
I would just use /bin/run-parts on project /cron subdirs, but didn't manage to split logs as I wanted. And splitted logging is very nice feature to have.
So, I just thought this kind of systems were created many times before, is there any ready to use solution for PHP project? Basically it's just some more smart run-parts equivalent. Should just write it once again?
P.S. There are many more job-queue specific solutions like Gearman. They are great, but this quesion is about project cron jobs lightweight solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们在我目前的工作中采取了略有不同的方法。我们使用 Jenkins(以前称为 Hudson)来满足 PHP 相关的调度需求。这很好,因为您可以利用现有的基础设施进行通知(jabber、电子邮件等),并且它与我们用于代码发布的其他构建作业并存。还有实时观察控制台输出、获取每次运行的记录等的能力。
我最近记录了我们组织 PHP 作业的方式,以便我们可以轻松地从 CLI 中利用我们的应用程序框架,这就是 Jenkins 与作业交互的方式。
以下是关于组织 PHP 批处理作业以与 Jenkins 或 Hudson 一起使用的帖子:
http://blog.shupp.org/2011/03/15/organizing-php-batch-jobs/
We've taken a slightly different approach at my current job. We use Jenkins (formerly Hudson) for our PHP related scheduling needs. It's nice because you can leverage the existing infrastructure for notifications (jabber, email, etc), and it sits along side our other build jobs for code releases. There's also the ability to watch console output in real time, get transcripts of every run, etc.
I documented the way we organize our PHP jobs recently so that we can easily leverage our application framework from CLI, which is how Jenkins interfaces with the jobs.
Here's the post about organizing PHP batch jobs for use with Jenkins or Hudson:
http://blog.shupp.org/2011/03/15/organizing-php-batch-jobs/
Periodic 是一个用 PHP 编写的 CRON 兼容任务管理器。为了使其按照您的意愿工作,仍然需要做一些工作,但它应该为您提供良好的基础。
Periodic is a CRON compatible task manager written in PHP. In order to make it work like you desire it, there will still some work to be done, but it should give you a good basis.
使用这个函数:
Use this function: