PHP CLI Security Interrupt exit

Source: Internet
Author: User
A PHPCLI small program, with

while (1) {
....
}

Way to handle queue tasks (mostly written to MySQL), ask if the console kill the process

How to safely interrupt, accept kill (or CTRL + Z in the foreground) signal
Finish a task, output Byebye and then exit

Thank you

Reply content:

A PHPCLI small program, with

while (1) {
....
}

Way to handle queue tasks (mostly written to MySQL), ask if the console kill the process

How to safely interrupt, accept kill (or CTRL + Z in the foreground) signal
Finish a task, output Byebye and then exit

Thank you

If it is called when the script terminates, you can use Register_shutdown_function () or __destruct (), but the problem obviously requires process control, the signal processor.

CTRL-C sends the SIGINT signal, the default process ends, but the process itself can redefine the behavior of receiving this signal.

Ctrl-z send Sigstop signal, the process is just stopped, and then send Sigcont signal, the process continues to run, involving the command has JOBS,FG,BG.

Refer to the Process Control extension predefined constants, http://php.net/manual/zh/function.pcntl-signal.php gives a piece of code, the reference is as follows:


  
   

针对你的问题的面向过程式示例代码,当然也可以采用面向对象方式:


   
    

以上例子当执行ctrl+c或者kill时会输出

➜  ....^Cctrl+cshutdown➜  .........killshutdown

Ps-ef | grep php
will list the current working script and see the PID for that path
then Kill-9 {pid}

Don't know if there's a better way.

  • 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.