Build a stable and reliable PHP daemon
what
##● pcntl_fork● Process/Process Group/Group Long process/session●Standard input/output/errorwhy
how
You need to install the pcntl extension to use it
● Background runningif ($pid=pcntl_fork ()) exit (0);// is the parent process, end the parent process , the child process continues●Leave the control terminal, log in to the session and process groupposix_setsid ();//The child process upgrades the group leader process, and leaves the original session/terminal● Prohibit the process from reopening the control terminalif ($pid=pcntl_fork ()) exit (0);//End the first child process and the second child process continues (the second child process is no longer a session Team leader) ● Close the open file descriptor fclose (STDIN), fclose (STDOUT), fclose (STDERR) // Close standard input and output and error display. ● Change the current working directorychdir("/")● Reset the file creation maskumask (0);//Prevent Inherit the mask left by the parentRelated recommendations: "PHP Tutorial"
The above is the detailed content of Use PHP to build your own stable and reliable daemon. For more information, please follow other related articles on the PHP Chinese website!