Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a PHP script loop forever doing computing jobs from a queue system

Tags:

php

amazon-sqs

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?

like image 499
dropson Avatar asked Dec 07 '22 03:12

dropson


2 Answers

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).

like image 115
Rich Adams Avatar answered May 20 '23 01:05

Rich Adams


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.

like image 25
Justin Ethier Avatar answered May 20 '23 02:05

Justin Ethier