This article mainly introduces the PHP process signal processing, has a certain reference value, now share to everyone, the need for friends can refer to
<?phpwhile (1) { echo "I am doing something important\n"; echo "If I am interruptted, the data would be corrupted\n"; echo "Be careful\n"; echo "\ n"; Sleep (5);}
Assuming that there is such a PHP script now, we want the script to output a piece of information and end the script when it receives a process signal such as SIGINT, Sigterm, and so on.
Using the Pcntl_signal and Pcntl_signal_dispatch functions (Note that PHP needs to install the PCNTL extension to use these two functions), see Code:
<?php/** * Signal ID and Signal name correspondence, the not commented out signal is can be registered through the pcntl_signal function */$GLOBALS [' g_sig_map '] = Array (1 = ' SIGHUP ', 2 = ' SIGINT ', 3 = ' Sigquit ', 4 = ' Sigill ', 5 = ' SIGTRAP ', 6 = ' Sigabr T ', 7 = ' sigemt ', 8 = ' SIGFPE ',//9 = ' SIGKILL ', ten = ' Sigbus ', one by one ' SIGSEGV ', ' sigsys ', ' sigpipe ', ' sigalrm ', ' SIGTERM ', + = ' Sigurg ',//17 = ' SIGSTOP ', ' sigtstp ', ' sigcont ', ' the SIG ' Chld ', + = ' sigttin ', ' Sigttou ', ' SIGIO ', ' sigxcpu ', 25 = ' Sigxfsz ', ' sigvtalrm ', ' sigprof ', ' sigwinch ', ' + = ' SIG INFO ', ' SIGUSR1 ', + = ' SIGUSR2 ',);/** * process signal processing function * @param int $signo signal ID */function Signalhan Dler ($signo) {if (ISSet ($GLOBALS [' G_sig_map '] [$signo]) {echo "caught signal {$GLOBALS [' g_sig_map '] [$signo]}, exit\n"; } else {echo "Caught unknown signal: $signo"; } exit ();} foreach ($GLOBALS [' G_sig_map '] as $id = + $name) {pcntl_signal ($id, ' Signalhandler ');} while (1) {echo "I am doing something important\n"; echo "If I am interruptted, the data would be corrupted\n"; echo "Be careful\n"; echo "\ n"; Pcntl_signal_dispatch (); This function call must be placed in the Loop body sleep (5);}
Executes the script in the Linux shell, and then sends a signal to the process using the KILL-2 script process ID
command, and the script outputs the information normally and exits.