10

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

6 Answers 6

6

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: https://www.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
Sign up to request clarification or add additional context in comments.

7 Comments

I like this idea. I'm trying it out now.
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?
You shouldn't need to use cron, it will run indefinately - if you use cron you'll end up with a lot of processes doing the same thing. Just chmod +x the bash script and start it up: /path/to/script.sh &
Sweet, got it working, thanks a bunch everyone, and especially you Eddy!
Actually one more thing. Do you know how I can keep it running after I've closed the terminal window? I put an & at the end, but it still stopped the process after I left.
|
2

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.

Comments

2

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

Comments

2

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

Comments

0

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

1 Comment

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>&1 &"); Or would that still keep creating scripts and never getting to the end of any?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.