问题
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?
回答1:
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 ;)
回答2:
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:
- Send a batch of emails to new users. (every 5 minutes?)
- Update user statistics (every hour?)
- Perform resource-intensive operations when the servers aren't slammed with traffic (Every night at midnight, or once a week on sunday nights?)
- Anything other event that doesn't occur in response to a user request (what Jeff calls "out of band").
回答3:
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 using Zend_Loader_Autoloader
(ZF >= 1.8) or Zend_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.
回答4:
you may also be intersted in how to store cron jobs with ZF
回答5:
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 :-)
来源:https://stackoverflow.com/questions/819148/getting-started-with-cron-jobs-and-php-zend-framework