The general server program is running in the system background, which is very different from the normal interactive command-line program. There is a function daemon in glibc. Call this function, you can make the current process out of the terminal into a daemon, the concrete content see man daemon. There is no such function in PHP, of course, if you are interested, you can write a PHP extension function to implement.
There are 2 ways to implement daemons in the PHP command-line program:
First, the use of nohup
> log.txt &
The daemon is implemented here.
PHP myprog.php is executed separately, and when CTRL + C is pressed, the program execution is aborted, and the current process and child processes are kill.
PHP myprog.php &, so that the execution of the program, although also to run in the background, is actually dependent on the terminal, when the user exits the terminal process will be killed.
Second, the use of PHP code to achieve
functionDaemonize () {$Pid=Pcntl_fork ();if ($Pid==-1) {Die"Fork (1) failed!\n");}ElseIf ($Pid>0) {To let a user-initiated process exitExit0);}Create a new session that is different from the terminal to disengage from the terminalPosix_setsid ();$pid = pcntl_fork (); if ($pid == -1) {die (elseif ($pid > 0" {//the parent process exits, leaving the child process as the final independent process exit (0);}} daemonize (); sleep (1000);
With the above code can be implemented daemon, when your PHP program needs to be run in the background, only need to call a packaged function daemonize ().
Note: There is no redirection for standard input and output here.
PHP Program Daemon Process