Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping a PHP Script

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

like image 575
William W Avatar asked Jul 13 '09 20:07

William W


People also ask

Does PHP have for loop?

For loop is a control structure that repeats a block of code as long as a condition is met. It's usually used to repeat a block of code a certain number of times. The PHP for loop function can be used to iterate over a set of code for a set number of times.

Which loop is best in PHP?

For-Each loop is best when you have to iterate over collections.

What is the syntax of for loop in PHP?

Syntax. Parameters: init counter: Initialize the loop counter value. test counter: Evaluated for each loop iteration.


4 Answers

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
like image 141
Eddy Avatar answered Oct 08 '22 11:10

Eddy


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
like image 40
Ben Blank Avatar answered Oct 08 '22 11:10

Ben Blank


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

like image 20
RC. Avatar answered Oct 08 '22 12:10

RC.


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.

like image 23
AnalyticaL Avatar answered Oct 08 '22 11:10

AnalyticaL