0

I need to have a PHP script running indefinitely, but I can't seem to find a good way to do this. My server runs on Windows Server 2008.

Do you think I can rely on Timer Tasks? Is there a better alternative?

Thanks in advance.

Olsi
  • 929
  • 2
  • 12
  • 26

1 Answers1

-1

If you need the script to run constantly, rather than being started periodically to do whatever job it does, then you need it to enter an endless loop, thus:

while (1) {
  do_something();
  sleep(1);
};

Do not omit the sleep() statement, unless you don't mind the script eating your entire processor.

Aaron Miller
  • 3,692
  • 1
  • 19
  • 26
  • Thank you Aaron. Do you think this, combined with a scheduled task (just to have it run at startup in case of a restart), can be a good solution in terms of resources? – Olsi Jul 30 '13 at 15:17
  • @Olsi You'll want to make sure, of course, that the task runs only at startup, or else have your PHP code check for an already-running instance and exit if it finds one. Other than that, the `sleep(1)` will cause the script to yield whatever CPU time the scheduler would give it other than what it actually needs to do its job. The only remaining concern would be memory usage; keep in mind that whatever RAM the PHP interpreter allocates for your script will remain allocated until the script exits, so memory leaks that ordinarily wouldn't matter could easily become a major problem here. – Aaron Miller Jul 30 '13 at 15:35