patrick_vane
patrick_vane

Reputation: 55

PHP-FPM (7.4.2) doesn't stop gracefully?

During updating, we want to stop php-fpm and wait for all running scripts to be finished before we make any file changes.

We found out that we needed to set process_control_timeout, so we placed "process_control_timeout = 36000s" in "/etc/php/7.4/fpm/pool.d/zz-00-overrides.ini" (and we restarted php-fpm).

Then we created a test script to test it out. Our test script creates a file, then 30 seconds later, it creates another file. The script:

$id = random_int(10000, 99999);
file_put_contents(__DIR__ . '/' . $id . '-start', '');
sleep(30);
file_put_contents(__DIR__ . '/' . $id . '-end', '');

When we run the script normally (browser -> nginx -> php-fpm), it creates the 1st file, 30 seconds later it creates the 2nd file.

When we run the script, wait a few seconds, and then try to stop it (run the same way as before: browser -> nginx -> php-fpm) (stopped by: "service php7.4-fpm stop"), it will create the 1st file, then the service stop command is run which only takes 2-3 seconds, then the browser says "502 Bad Gateway", and then the 2nd file is never created.

It doesn't gracefully stop. The desired outcome for us is that "service php7.4-fpm stop" waits for all the scripts to be done, and then stops, instead of it killing off any running scripts the way it is doing now in order to forcefully stop.

Are we missing something, are we doing something wrong? Is it a bug somewhere somehow? Any help would be really appreciated.

Upvotes: 2

Views: 2830

Answers (1)

patrick_vane
patrick_vane

Reputation: 55

Running kill -QUIT $(cat /run/php/php7.4-fpm.pid) does take the process_control_timeout config in account. It will cause the PHP-FPM process to stop as soon as all the scripts have finished their execution. At that point the PID will be removed. So, in order to make it work:

  1. run $(kill -QUIT $(cat /run/php/php7.4-fpm.pid))
  2. in a loop, check if /run/php/php7.4-fpm.pid still exists, if not, break the loop.

Upvotes: 2

Related Questions