0

To monitor the max_execution_time of a particular script, I use

ini_set('max_execution_time', 600); //600 seconds = 10 minutes

at the top of my script. However, when I build a logging of the same file it returns me the following:

25-09-2017 23:08:01|STARTED
25-09-2017 23:09:01|ALREADY RUNNING
25-09-2017 23:10:01|ALREADY RUNNING
...................|ALREADY RUNNING
26-09-2017 01:09:01|ALREADY RUNNING
26-09-2017 01:10:01|ALREADY RUNNING
26-09-2017 01:11:01|STARTED
26-09-2017 01:12:01|ALREADY RUNNING
26-09-2017 01:13:01|ALREADY RUNNING
...................|ALREADY RUNNING
26-09-2017 03:10:01|ALREADY RUNNING
26-09-2017 03:11:01|ALREADY RUNNING
26-09-2017 03:12:01|STARTED
26-09-2017 03:13:01|ALREADY RUNNING
26-09-2017 03:14:02|ALREADY RUNNING
...................|ALREADY RUNNING
26-09-2017 05:09:01|ALREADY RUNNING
26-09-2017 05:10:02|ALREADY RUNNING
26-09-2017 05:11:01|STARTED
26-09-2017 05:12:01|ALREADY RUNNING
26-09-2017 05:13:02|ALREADY RUNNING
26-09-2017 05:14:01|ALREADY RUNNING
...................|ALREADY RUNNING
26-09-2017 07:14:01|ALREADY RUNNING
26-09-2017 07:15:01|ALREADY RUNNING
26-09-2017 07:16:01|STARTED

The ...................|ALREADY RUNNING represents multiple messages repeating every minute in between.

What am I doing wrong, since the interval is not 10 minutes by far?

EDIT:

The while loop I have:

while (true) {
    //DO PROCESSING WHEN FILES ARE PRESENT
    Sleep(1);
    }
TVA van Hesteren
  • 1,031
  • 3
  • 20
  • 47
  • Have you checked what your actual timeout limit is with phpinfo(), and loading the page with a browser? If you're running php-fpm and nginx or anything out the old "Apache and PHP" confines, the actual time limits might be set, and override PHP's own php.ini, else where. – DocWeird Sep 26 '17 at 07:58
  • @DocWeird, I run the script with a cronjob. The phpinfo states that the max. execution time is set on 30 for both the local and master value. – TVA van Hesteren Sep 26 '17 at 08:03
  • 1
    Are you using sleep -command in the script? IIRC using sleep does not increase the execution time. – DocWeird Sep 26 '17 at 08:28
  • @DocWeird, yes I have a while(true) loop with a sleep of 1 second at the end of the loop. So, how can I overcome this? I use the Sleep to prevent CPU from rising to 100% when the while loop has nothing to perform (it processes files when present). See the edit in my question for the while loop example – TVA van Hesteren Sep 26 '17 at 08:33
  • 1
    Hmm... How about using two scripts? A main script that will call the script which processes files *and* sleeps. That way the main script should still time out while the called script will happily go about sleeping without concerns about execution times. – DocWeird Sep 26 '17 at 09:05
  • @DocWeird, to check your statement I decreased the max execution time to 2 minutes and removed the sleep. It now restarts every 2 minutes as expected. Thanks for your great answer regarding the sleep not counting towards the Max Execution time. The idea of multiple scripts might be a great idea, however it will be eating resources, since I have to start a new process for each file to process? Sometimes I receive over 100 files at once to process immediately, which results in 100 new commands triggering new instances of the processing script meaning that 100 processing script will start? – TVA van Hesteren Sep 26 '17 at 09:10

1 Answers1

1

Just check execution time manually.

$max_exec_time = 600;

$start_time = time();
while (true) {
  do_something();
  sleep(1);
  if (time() - $start_time > $max_exec_time) {
    exit;
  }
}
Dmitri
  • 668
  • 4
  • 6
  • That is actually a great idea! Thanks..! Any disadvantages known for this approach to consider? – TVA van Hesteren Sep 26 '17 at 09:29
  • 1
    Only one I can think of is that it won't exit until do_something() is completed. So you should probably set max_execution_time too, just in case. – Dmitri Sep 26 '17 at 09:44