简体   繁体   中英

PHP script cannot be exit() in CLI mode

I have a crontab script which was executed by run-parts every 10 minutes

sudo -u www php -f /path/to/parser.php crawl_content1 >> /tmp/job_1.log 2>&1 &
sudo -u www php -f /path/to/parser.php crawl_content2 >> /tmp/job_2.log 2>&1 &

my parser.php contians following code

$max_execute_time = time() + 9*60;
while(true)
{
   //... do something ...
   if(time() >= $max_execute_time)
   {
       echo "time out!";
       exit;
   }
}

my question is exit part. after exit, the script still runing as 10% of CPU and 25% of memory (512MB). After I changed exit to break, problem solved.

Is there anyone know what's the problem about this? Thanks

This is a tangential response, but I find it odd that you implemented your own max execution timer, when PHP has at least three ways of doing it for you (saving you the trouble and CPU cycles of calculating it yourself in the script):

sudo -u www php -d max_execution_time=30s -f /path/to/parser.php crawl_content1 >> /tmp/job_1.log 2>&1 &

Or, at the top of your script:

ini_set('max_execution_time', 30);

Or (equivalent):

set_time_limit(30);

(You can also modify php.ini , but you understandably probably don't want to.)

I only suggest this because your implementation terminates your whole script ; there are other instances in which the execution time of a loop might be legitimately limited without terminating the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM