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.

3
  • Do you have cron on your server? This is the common tool to trigger any kind of script time-based. Commented Feb 16, 2011 at 16:25
  • Do you have a commandline? Type there "which crontab" Commented Feb 16, 2011 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 Commented Feb 16, 2011 at 16:32

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How do you put PHP script to sleep?
There's a built-in sleep() call, as well as a few others ("See Also" at the bottom of that doc page)
4

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

Comments

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.

Comments

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.

3 Comments

@miki725: php.net/manual/es/function.sleep.php (sorry, karma too low to comment the Adam Answer)
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?
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.
1

You are looking for cron job

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.