views:

189

answers:

5

Currently I have a perl script that runs forever on my server, checking a SQS for data to compute. This script has been running for about 6 months with no problems what so ever.

So, now I want to switch over to PHP'S CLI, and have the script loop there forever. Mostly because I'm more familiar with PHP.

Basicly,

$i="forever";
while($i==="forever"){
    doSomething();
    sleep(10);
}

The script will do a shell_exec("/shell_script.sh"); that can take up to 2 hours to process. Will this trigger a max execution time or similar?

Is this "ok" to do? If not, what is the alternatives?

+5  A: 

Personally, I would use Python or Perl for a task such as this. PHP is really more of a domain-specific language for writing web sites and services.

Justin Ethier
PHP is definitely not the best tool for the job, but it does work -- I've written several scripts like this, and they run for months without issue. Usually. You do have to be careful about memory usage. And if your organization is proficient in PHP, and not much else, there's something to be said for not introducing a new language. In the ideal world, sure, you'd always use the best tool for the job, and you'd have time to learn new languages, but that isn't always the reality.
Frank Farmer
What language would be the best one for this kind of script?
dropson
It depends what you are familiar with. Personally I like Python because it's easy to learn but lets you quickly write solid, working code. Also it tends to be easy to maintain because whitespace as syntax and the philosophy of having *one* way to any particular task means code tends to be uniform.
Justin Ethier
+4  A: 

You will need to make sure that you set the maximum execution time of the script to zero, so that no time limit is imposed. You can do this from the PHP file with,

set_time_limit(0);

Or you can set it in your php.ini file. (See PHP manual on set_time_limit for more information). Other than that, your approach should work fine.

However, there's no need to set a variable and check it in order to loop forever,

$i="forever";
while($i==="forever")

You can simply just do while(true) or while(1).

Rich Adams
Great answer. Thanks alot!
dropson
A: 

If all you wanted to do was have an endless loop then

$i=1;
while($i===1){
    doSomething();
    sleep(10);
} 

Simply saying as long as $i is 1, script will keep going and $i never changes so... Only problem is that you might have an execution time limit so if you do then use this or similar

for($i=1; $i<100; $i++){
    doSomething();
    sleep(10);
} 
header("Location: samepage.php:);

EDIT Ignore my post Use http://stackoverflow.com/questions/2977105/having-a-php-script-loop-forever-doing-computing-jobs-from-a-queue-system/2977163#2977163

Neb
+1  A: 

I'm not very familiar with unix, but I think people can use "cron" to do routine tasks. Write your tasks in scripting language of your choice, and have it executed periodically by Cron.

Andree
For anything that might cycle a few times a minute or more, cron isn't all that great. You have to worry about the last invocation not completing before the next one starts, etc. For jobs that only run a few times an hour, cron is great though.
Frank Farmer
+1  A: 

Just a side-note as other have suggested to turn of the time limit via set_time_limit:

Due to endless possibilities of using PHP in shell environments, the maximum execution time has been set to unlimited. Whereas applications written for the web are often executed very quickly, shell application tend to have a much longer execution time.

Using PHP from the command line >> Differences to other SAPIs

So actually running PHP scripts on the command line do not suffer from the execution time limit.

Stefan Gehrig
Ok, that is good to know.
dropson