cron 作业和 PHP (Zend Framework) 入门
我对 cron 作业这个主题完全陌生,所以我不知道从哪里开始学习它们; 何时、为何或如何将它们与我的 Zend Framework 应用程序或一般的 PHP 一起使用。
任何人都可以通过示例解释该过程,或者推荐一些好的资源来入门吗?
I am totally new to the subject of cron jobs so I have no idea where to start learning about them; when, why, or how to use them with my Zend Framework application, or PHP in general.
Can anyone explain the process, with an example, or recommend some good resources to get started?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Cron 作业是 Linux 操作系统中自动执行任务的机制。 与 Zend Framework 关系不大。 不过,该框架可以帮助您在 php 中开发高级 cron 任务。 但随后您必须在 shell 中设置您的 cron 作业。
谷歌搜索“如何设置 cron 作业”在顶部显示了此链接:
http://www .adminschoice.com/docs/crontab.htm
我相信这篇文章会对您有所帮助。
PS
作为执行命令,您应该输入如下内容:
/usr/local/bin/php -f
其中第一个路径是 php cli 可执行文件的完整路径,可能有所不同在你的机器上。 您可以通过发出以下命令来确定:
which php
祝 cron 作业好运;)
Cron jobs is a mechanism to automate tasks in Linux operating system. And has pretty little to do witn Zend Framework. The framework can help you develop an advanced cron task in php though. But then you will have to set up your cron job in the shell.
Googling for "how to set up cron job" revealed this link at the top:
http://www.adminschoice.com/docs/crontab.htm
I'm sure this article will help you.
P.S.
As a command to execute you should put something like:
/usr/local/bin/php -f <path_to_your_php_script>
where the first path is the full path to your php cli executable, which may differ on your machine. You can make sure by issuing this command:
which php
Good luck with cron jobs ;)
也许一个现实生活中的例子会有所帮助。 几年前,我使用 Zend Framework 开发了一个事件日历项目。 在此日历中,用户可以创建事件并将 1 个或多个日期附加到该事件。 当然,我将其实现为数据库中的多对一连接,但这意味着为了附加日期,事件必须首先存在。 不过,我希望允许用户在创建事件时添加日期,而不是在创建事件之后添加日期。 换句话说,我希望用户同时编辑事件的所有方面,并且仅在单击“保存”时才提交。
我通过在用户开始创建事件记录时将新的空事件记录插入数据库来解决该问题。 当用户单击“保存”时,该空记录将被填充并保存,或者当用户单击“取消”时被删除。 当用户没有单击“取消”就离开时,就会出现问题,数据库中会留下空的事件记录。 最终,数据库将充满这些无意义的空事件,事情可能会变得丑陋。
我编写了一个名为“maintenance()”的函数,除其他外,它删除了所有超过 24 小时未保存的记录。 我设置了一个每晚运行的 cron 作业,并执行了一个运行 Maintenance() 的命令行 php 脚本。
您可能会使用 cron 作业执行的其他操作:
Maybe a real-life example would help. A few years ago, I worked on an event calendar project using the Zend Framework. In this calendar, a user could create an event and attach 1 or more dates to the event. Naturally, I implemented this as a many-to-one join in my database, but this meant that in order to attach a date, the event had to exist first. However I wanted to allow users to add dates while they were creating the event, not after they created an event. In other words, I wanted the user to edit all aspects of an event at the same time, and submit only when they clicked "save."
I solved the problem by inserting a new empty event record into the database when the user begins creating an event record. This empty record gets filled in and saved when the user clicks "save", or is deleted when the user clicks "cancel". The trouble occurred when users navigated away without clicking "cancel", and the empty event record was left in the database. Eventually, the database would fill up with these meaningless empty events, and things might get ugly.
I wrote a function called "maintenance()", which, among other things, deleted all unsaved records older than 24 hours. I set up a cron job that ran nightly and executed a command-line php script that ran maintenance().
Other things that you might use a cron job for:
由于 Zend Framework 是某种松散耦合组件的集合,因此您可以在每个 PHP 脚本(包括 CLI 脚本)中使用您喜欢的组件。 不过,您必须确保 PHP 可以找到相关的 Zend Framework 类,因此您必须将 Zend Framework 库路径添加到
include_path
中。 然后,您可以使用Zend_Loader_Autoloader
(ZF >= 1.8) 或Zend_Loader
(ZF < 1.8) 设置自动加载,以使生活更轻松。 现在,您应该可以访问您可以随意使用的所有 Zend Framework 组件。与所有其他脚本或程序一样,这个 PHP 脚本自然可以在 cron 作业中使用。 另一件需要注意的事情是,您必须确保运行 cron 作业的用户具有对 Zend Framework 库路径的读取访问权限,否则 PHP 无法读取 Zend Framework 文件。
如果您的问题是针对 CLI 脚本中 MVC 组件的使用,我必须承认我对此没有任何经验。 我认为必须付出不可忽视的努力来实现适当的请求类、响应类、调度程序类和路由类。
编辑:
请参阅这篇文章了解更多信息。
As the Zend Framework is a collection of somehow loosely-coupled components you can use the components you like in every PHP script including CLI scripts. You'll have to make sure though, that PHP can find the relevant Zend Framework classes so you have to add the Zend Framework library path to your
include_path
. Then you can setup the autoloading usingZend_Loader_Autoloader
(ZF >= 1.8) orZend_Loader
(ZF < 1.8) to make life easier. Now, you should have access to all Zend Framework components that you can use at will.Like every other script or program this PHP script can be used in a cron job naturally. One more thing to note is, that you'll have to make sure that the user running your cron jobs has read-access to the Zend Framework library path otherwise PHP can not read the Zend Framework files.
If you're question was targeted on the use of the MVC component in CLI scripts, I must admit that I don't have any experience on this. I would suppose that a not negligible effort must be made to implement appropriate request-, response-, distpachter- and route-classes.
EDIT:
Please see this article for more information.
您可能还对如何使用 ZF 存储 cron 作业
you may also be intersted in how to store cron jobs with ZF
以下是使用 Zend Framework 实现 cron 系统的好方法的链接:
Zend Framework 应用程序中的 Cron 任务
就我个人而言,我已经对其进行了调整,因此参数可以是动态的,但基础是相同的:-)
Here's a link to a good way of doing your implentation of a cron system with Zend Framework:
Cron tasks in Zend Framework apps
Personnaly, I've adapted it so parameter could be dynamic, but the base is the same :-)