Odd behavior of php sleep function

We have inherited a platform in which there is a crobjob, which every minute twists the local php script three times with different parameters ( curl -s -o --url https://localhost/myscript.php?option=XYZ -k). That the script runs for about 1 minute, and its possible multiple instances with the same overlapping options for some time. The script is logged in a different file for each parameter, and each log is run with a time stamp when the script starts, so it acts as an instance identifier.

The script has this skeleton:

<?php

$option=XYZ;
$scriptId = time();
$file = "log_$option.txt";

file_put_contents($file,"\n$scriptId: Start\n",FILE_APPEND);
session_start();

$expires = time()+60;
file_put_contents($file,"\n$scriptId: Expires at $expires\n",FILE_APPEND);

while(time()<$expires){

    file_put_contents($file,"\n$scriptId: Not expired at ".time()."\n",FILE_APPEND);

    switch($option){
        case X:
            do_db_stuff();
            break;
        ...
    }

    file_put_contents($file,"\n$scriptId: Will sleep at ".time()."\n",FILE_APPEND);
    sleep(13);
    file_put_contents($file,"\n$scriptId: Woke up at ".time()."\n",FILE_APPEND);
}

file_put_contents($file,"\n$scriptId: Finished at ".time()."\n",FILE_APPEND);

Usually this script works fine (even if they overlap when instance A is sleeping for the last time and starts instance B), but sometimes we have two problems that we can confirm with the logs:

  • 13 (a 13);
  • script ( "", , ). [ 2]

, :

  • php max_execution_time 240 , script ;
  • sleep , , curl cookie, ( , , script, );
  • , php , .

, . , , ? ?

:

  • Linux
  • mysql 5.5
  • php 5.3
  • php max_execution_time 240

1:. : 3 , 3 , . ( ).

Edit2: @Jan, . script :

[2016-01-05, 13:11:01] Will sleep at 2016-01-05, 13:11:29
[2016-01-05, 13:11:01] Woke up at 2016-01-05, 13:11:37 with sleep return 5
[2016-01-05, 13:11:01] Not expired at 2016-01-05, 13:11:37
[2016-01-05, 13:11:01] Will sleep at 2016-01-05, 13:11:37
[2016-01-05, 13:11:01] Woke up at 2016-01-05, 13:11:38 with sleep return 13
... no more log from instance [2016-01-05, 13:11:01] ...
[2016-01-05, 13:12:01] Start

sleep:

, sleep() . Windows 192 ( WAIT_IO_COMPLETION Windows API). , .

, , sleep - .

, (pcntl_signal?), , ?

Edit3: pcntl_signal ( 1 255) , , .

+4
2

pcntl_signal.

, . AFAIK , .

pcntl_alarm . PHP - PCNTL

+1

, apache. , Apache PHP , Apache , script.

cron , , PHP , apache ? CLI php.ini max_execution_time.

0

Source: https://habr.com/ru/post/1622901/


All Articles