This article describes the example of PHP using the process as a daemon process. Share it with everyone for your reference. The specific analysis is as follows:
Usage of posix_setsid() in php
The documentation explanation is “Make the current process a session leader”
Reference documentation: http://linux.die.net/man/2/setsid
It means that the process that calls this function between a process group (parent process and child process) will be elected as the leader of the process group
So the way to make a process a daemon is:
1 fork out a child process
2 In the child process posix_setsid()
3 Exit the parent process
There is an example in the documentation:
<?php $pid = pcntl_fork(); // fork if ($pid < 0) exit; else if ($pid) // parent exit; else { // child $sid = posix_setsid(); if ($sid < 0) exit; for($i = 0; $i <= 60; $i++) {//do something for 5 minutes sleep(5); } } ?>
I hope this article will be helpful to everyone’s PHP programming design.