views:

558

answers:

6

I've got a PHP script that checks a directory and deletes any files not modified within 15 seconds (It's for a game).

My problem is how to get this script to run all the time. I set up a cron job to run every 10 minutes and then in the PHP script I have an infinite loop with a sleep(10). My thought was that it would run the code every 10 seconds, and in the case the script stopped, the cron job would restart it eventually.

However, after the script is started, it runs for about 3 loops (30 secs) and then stops. I've heard PHP only gets so much memory per file load.

How can I make this PHP script loop indefinitely? Maybe there is some way to call itself

+1  A: 

You might want to check your max_execution_time parameter in the php.ini file. I believe the default is 30 seconds. The way you have it setup with cron, you will probably have multiple instances of the script running after 10 minutes unless you add some logic in the script to check that an instance of itself is not already running

RC
I am already checking for that.
William
A: 

PHP as a language is not very good at running indefinitely. Since you have a cron-job every ten minutes, why not execute your task 60 times and then exit?

Also, PHP has different config files for CLI and for apache modes on most linux servers. So it might be wise to check your etc/php/cli/php.ini and check maximum execution time and memory limits.

AnalyticaL
+2  A: 

Make sure your PHP executable is compiled for use as a command-line interpreter, not a CGI executable. PHP normally kills scripts which run for longer than max_execution_time (default is 30 seconds). CLI executables, however, do not impose this limitation.

More info about CLI vs. CGI SAPIs.

You can check your executable's SAPI by using the --version argument:

$ php --version
PHP 4.3.0 (cli), Copyright (c) 1997-2002 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies
Ben Blank
A: 

every so often (before you run out of memory) break out of the loop and re-execute itself exec("/usr/bin/php " . FILE);

buck
The problem with that is the parent script waits for a return, which would never happen.Would this work though:exec("/usr/bin/php ./servercleaner.php > /dev/null 2>Or would that still keep creating scripts and never getting to the end of any?
William
A: 

You could run a parent php process that forks a client at an interval. If you're curious about exploring it as an option here is a good starting point: http://ca2.php.net/pcntl Nice thing about doing it this way is that the parent process can kill client pids that do not end within a reasonable amount of time.

If you're looking for something quick and dirty you could write a bash script to invoke the php quite easily (if you're on linux):

#!/bin/bash
while [ "true" ]; do
        /path/to/script.php
        sleep 15
done

EDIT You don't really even need the script, bash will do it all on one line:

while [ "true" ]; do /path/to/script.php; sleep 15; done
Eddy
I like this idea. I'm trying it out now.
William
I'm having trouble getting the bash script to run via the cron. Is there any special thing I need to put, or just the timing and then the path to the script?
William
Eddy
Sweet, got it working, thanks a bunch everyone, and especially you Eddy!
William
William
nevermind, adding nohup did the trick.
William
A: 

There is a PEAR module for writing long-running PHP scripts. I've not used it myself though.

Ken Keenan
Broken link .
Bobby Jack
Works OK for me
Ken Keenan