tags:

views:

380

answers:

9

I have a PHP script that needs to be run at certain times every weekday. Is cron or Windows task scheduler the only way to do this?
Is there a way to set this up from within another PHP script?

+4  A: 

Depends how exact the timing needs to be. A technique I've seen used (dubbed "poor man's cron") is to have a frequently accessed script (a site's home page, for example) fire off a task on the first page load after a certain time (kept track of in the database).

If you need any sort of guaranteed accuracy, though, cron or a Windows scheduled task is really the best option. If your host doesn't support them, it's time to get a better one.

ceejayoz
I would've said the exact same thing; nothing beats cron for this. +1
musicfreak
+1  A: 

Cron, of course, is by far the best way to schedule anything on *nix.

Ryan Oberoi
A: 

You probably want to use cron (or windows scheduled tasks).

If you really wanted, you could set up another php script to run continuously with an infinite loop (with a sleep command inside the loop, say for 30 seconds or so) and then when you reach your desired day/time execute the other script via a shell command call. While possible, I can't think if a single good reason to use this method rather than cron/scheduled tasks

James Conigliaro
A: 

You can write a long running script that runs your main script in predefined times but it will be very unnecessary, error prone, and it will basically be a "cron rewrite in phph".

Using the real cron itself will be easier and a more robust solution. If you are packaging an application, you can put a file in /etc/cron.d which contains a single cron line running your application.

hayalci
+2  A: 

Apart from cron or Scheduled Tasks, you can have your PHP script to always run it. The process should sleep (within a loop) until the time has reached. Then, the process could execute the remaining functions/methods. Or, you can set up another script (which will act as a daemon) to check for the time and execute the other script.

Alan Haggai Alavi
A: 

You'll need to use a cron job (under Linux/Unix) or a scheduled task under Windows. You could have another script running on a continuous basis which checks the time and executes a script at a specified interval, but using the OS-supplied mechanism is easier to manage and resilient to restarts, etc.

Rob
+2  A: 

Well since the web is a pull mechanism you have to have some sort of action that will trigger a PHP script to execute. cron is an option on *nix and task scheduler on windows. You could also write your own service that has a timer but only if needed, this is common on windows services for updaters, jobs etc.

One way you could do it is in the cron task just call a php script for each action needed. Or one php script that executes other tasks. The problem with web based tasks though such as PHP is timeouts. Make sure your tasks are under 60-90 seconds. If not you might look at using python , perl or ruby or even bash scripts to do the work rather than the PHP script.

cron seems like the best option for you though. You will have to call your script with wget. There are examples here: http://www.thesitewizard.com/general/set-cron-job.shtml

For instance this runs the script everyday at 11:

30 11 * * * /usr/bin/wget http://www.example.com/cron.php
Ryan Christensen
PHP can be run on the command line, which'll avoid timeout issues.
ceejayoz
Just run it from cron. Something like: 30 11 * * * /usr/bin/php -f /var/www/cron.php
fiXedd
+1  A: 

If this is in a remote server you do not have cron access to, you can setup cron/windows scheduler on your computer, to open a web browser to the page that contains the script you wish to run

lyrae
A: 

The Uniform Server project has some good instructions on mimicking cron in environments where cron is unacceptable. Still though, if cron is at all an option, use it.

dimo414