DEV Community

kakisoft
kakisoft

Posted on

PHP, Docker - How to enable PCNTL(Process Control Extensions)

Environment
Laravel version : 8.16.1
PHP version : 7.4.7

Sometimes PCNTL(Process Control Extensions) is required to use specific features in Laravel.

(PHP: PCNTL - Manual)
https://www.php.net/manual/en/book.pcntl.php

For example, if you want to specify the job execution timeout period in Laravel, this function must be enabled.

The following configuration and source code shows that timeout period has been set.
(You can set the job to terminate as an error after X seconds.)

There are two ways.
one is to specify it from the artisan command when executing the job, and the other is to write it in the source code.

When using the artisan command, it is as follows.

php artisan queue:listen --timeout=30
Enter fullscreen mode Exit fullscreen mode

When writing in source code, it is as follows.

namespace App\Jobs;

class ProcessPodcast implements ShouldQueue
{
    /**
     * The number of second that the job can execute until timeout period.
     *
     * @var int
     */
    public $timeout = 120;
}
Enter fullscreen mode Exit fullscreen mode

As an aside, when these are used both, the source code value takes priority.

It means, in the case of the above example, it will time out in 120 seconds.

But, to use this feature, PCNTL(Process Control Extensions) must be enabled as described above, otherwise the timeout period will be 60 seconds by default.

For more information, please refer to the link.
https://laravel.com/docs/8.x/queues#timeout

How do I enable that feature? when you look into that, you will find "Please compile PHP".
It will discourage you.
https://www.php.net/manual/en/pcntl.installation.php

PHP Official

Process Control support in PHP is not enabled by default. You have to compile the CGI or CLI version of PHP with --enable-pcntl configuration option when compiling PHP to enable Process Control support.

Stack Overflow
How to enable pcntl in php ( while using a framework like Symfony2 )
How to enable PCNTL on Ubuntu server 16.04 - Stack Overflow

It's a daunting task for Docker developers.

However, it is possible to enable PCNTL(process control function) by editing the Dockerfile without recompiling PHP.

To be specific, add the following syntax.

Docker image was designed to use the official php-fpm distribution.

RUN docker-php-ext-configure pcntl --enable-pcntl \
  && docker-php-ext-install \
    pcntl
Enter fullscreen mode Exit fullscreen mode

Example of editing the Dockerfile.

FROM php:7.4.11-fpm

# install composer
RUN cd /usr/bin && curl -s http://getcomposer.org/installer | php && ln -s /usr/bin/composer.phar /usr/bin/composer
RUN apt-get update \
&& apt-get install -y \
git \
zip \
unzip \
vim

RUN apt-get update \
    && apt-get install -y libpq-dev \
    && docker-php-ext-install pdo_mysql pdo_pgsql

RUN docker-php-ext-configure pcntl --enable-pcntl \
  && docker-php-ext-install \
    pcntl

WORKDIR /var/www/html
Enter fullscreen mode Exit fullscreen mode

If you are using Docker-composer, rebuild it with a command such as "docker-compose up -d --build".

To check if pcntl is enabled use the following command after login to the container.

php -i | grep pcntl
Enter fullscreen mode Exit fullscreen mode

If you see the following message, pcntl is enabled.

pcntl support => enabled
Enter fullscreen mode Exit fullscreen mode

See below for php command options.
PHP: Options - Manual

Discussion (0)