php-pcntlHow to dispatch PCNTL signals in PHP?
Dispatching PCNTL signals in PHP is a way to handle signals sent to a process. It can be used to catch and handle signals sent to the script, such as SIGTERM
or SIGINT
.
Example code
// Setup signal handler
declare(ticks = 1);
pcntl_signal(SIGTERM, "signal_handler");
// Signal handler function
function signal_handler($signal) {
switch($signal) {
case SIGTERM:
// Do something
break;
}
}
Output example
None
Code explanation
declare(ticks = 1)
: This tells PHP to check for any signals every time it executes a statement.pcntl_signal(SIGTERM, "signal_handler")
: This registers the signal handler function for theSIGTERM
signal.switch($signal)
: This checks which signal was sent to the script.case SIGTERM:
: This is the code that will be executed when theSIGTERM
signal is sent.
Helpful links
More of Php Pcntl
- How to install PCNTL for PHP in Debian?
- How to use PCNTL alarm in PHP?
- How to use pcntl_wifexited in PHP?
- How to use pcntl_wexitstatus in PHP?
- How to use pcntl_signal in PHP?
- How to check if PCNTL is enabled in PHP?
- How to use pcntl_wait in PHP?
- How to use pcntl_waitpid in PHP?
- How to get the process ID with PHP PCNTL?
- How to use pcntl_fork in PHP?
See more codes...