Building CLI applications quite often implies working with the system processes. PHP provides a powerful process control extension called PCNTL. The extension allows us to handle process creation, program execution, signal handling, and process termination. It only works on Unix-like machines, where PHP is compiled with the --enable-pcntl
configuration option.
To confirm that PCNTL is available on our system, we can execute the following console command:
php -m | grep pcntl
Given the power it bares, the use of the PCNTL extension is discouraged in production web environments. Writing PHP daemons scripts for command-line applications is what we want to use it for.
To start putting things into perspective, let's go ahead and see how we would use the PCNTL features to handle process signals.
PCNTL relies on ticks for its signal handling callback mechanism. The official definition (http://php.net/manual/en/control-structures.declare.php) of a tick says:
A tick is an event that...