Timed scripting PHP
I am trying to figure out how to do time scripting in PHP. Basically let's say in my application, I want to do 开发者_运维百科a task at timed intervals (e.g., send email notification to users everyday, do some database cleanup at certain times, etc.) How is this type of scripting (scheduling) is done in PHP? If not possible in PHP, then how to do it and in what language? I am both Linux and Windows sharing hosting accounts, so I would like this method if possible to be universal.
Any help is appreciated.
I just schedule cron jobs that run PHP scripts.
*/5 * * * * php /var/www/cron/cleanup-db.php
An alternate (Windows-compatible) approach would be to run a persistent PHP script which sleeps for an interval, and on wakeup it checks to see if any jobs need to be run. For example, check to see if any pending requests have not had a response or a reminder email in N hours.
On Unix machines, you use cron
, which is for re-occuring jobs. On Windows, the equivalent is at
Cron is the obvious choice but you may not be able to use it because you host your site on a "shared" environment. Try online services that generate auto http requests to your URLs based on the schedule you set. Google "schedule http request online", there are many of such services out there, some of them are free or have free options.
To complete Adam's answer, in Windows you have the chance to make Scheduled Tasks, wich can be programed to given intervals. That's the way we do.
The problem I see is that you are talking about shared hostings, probably you don't have rights to schedule task in that environment. In that case you should ask your system admin if such task is available.
There is another requirement: the task can be made in php if you have php-cli available, so check it out too.
You are looking for cron job
http://en.wikipedia.org/wiki/Cron
精彩评论