2

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.

miki725
  • 27,207
  • 17
  • 105
  • 121
  • Do you have cron on your server? This is the common tool to trigger any kind of script time-based. – powtac Feb 16 '11 at 16:25
  • Do you have a commandline? Type there "which crontab" – powtac Feb 16 '11 at 16:28
  • I have access to SSH, but no direct command line (terminal) access since it's a sharing hosting account. I tried running this command on SSH and it gave me `-bash: which: command not found` – miki725 Feb 16 '11 at 16:32

5 Answers5

5

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.

Annika Backstrom
  • 13,937
  • 6
  • 46
  • 52
4

On Unix machines, you use cron, which is for re-occuring jobs. On Windows, the equivalent is at

Marc B
  • 356,200
  • 43
  • 426
  • 500
2

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.

Carlos Mora
  • 1,164
  • 2
  • 12
  • 28
  • @miki725: http://php.net/manual/es/function.sleep.php (sorry, karma too low to comment the Adam Answer) – Carlos Mora Feb 16 '11 at 16:38
  • Does that function interfere with PHP max_execution time? So for instance, if I have a max_execution time of 100sec, and I want to put PHP to sleep for 200sec, would that give me an error? – miki725 Feb 16 '11 at 16:43
  • php-cli has it's own configuration, separated from mod_php, so it shoulsn't be a problem. As a sample, there are desktop programs written in PHP + a GUI like GTK, so the exec time is not a problem. – Carlos Mora Feb 21 '11 at 09:54
2

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.

1

You are looking for cron job

http://en.wikipedia.org/wiki/Cron

bensiu
  • 24,660
  • 56
  • 77
  • 117