Php background daemon + process signal processing
0) {exit (0); // the parent process exits} // The Child process becomes the new session leader posix_setsid (); // disables the first child process from opening the control terminal, you need to create a second sub-process. I do not know how it works. $ pid = pcntl_fork (); if ($ pid =-1) {exit ("fork2 failed! \ N ") ;}elseif ($ pid> 0) {exit (0) ;}echo" process id :". posix_getpid (). "\ n"; $ open = true; function sig_handler ($ signo) {global $ open; switch ($ signo) {// custom signal SIGUSR1case SIGUSR1: echo "Hello, I continue to run \ n "; break; case SIGTERM: echo". when this cycle ends, I close \ n "; $ open = false; break; default: # code... break;} pcntl_signal (SIGUSR1, 'sig _ handler'); pcntl_signal (SIGTERM, 'sig _ handler'); // This line of code is very important, in this way, the signal can be checked every time a low-level statement is executed, otherwise the process signal declare (ticks = 1) cannot be detected in an infinite loop; while ($ open) {echo date ('Y-m-d H: I: s '). "\ n"; sleep (1);} echo "I exited 88"; exit;