PHP Process Signal Processing

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.