Correct Implementation Method of php Process daemon

  • 2021-11-02 00:10:26
  • OfStack

Preface

daemon phonetic symbol: ['di: m], which means patron saint or elf in Chinese. In fact, it also means daemon.

The Daemon program is a straight-running server-side program, also known as a daemon. Usually run in the background of the system, no control terminal does not interact with the foreground, and Daemon program 1 is generally used as a system service. Daemon is a long-running process that usually runs after the system is started and ends when the system is shut down. 1 Generally speaking, Daemon program runs in the background because it has no control terminal and cannot interact with users in the foreground. Daemon Program 1 is generally used as a service program, waiting for the client program to communicate with it. We also refer to the running Daemon program as a daemon.

Each process has a parent process, and the child process exits, and the parent process can get the status of the child process exiting.

A daemon is simply a process that can run in the background without a terminal. This is a very common process in Linux. After services such as apache or mysql are started, they will enter the memory as daemons.

Taking PHP as an example, if I have a time-consuming task to run in the background: import all 20 million users in user table in mysql into redis for warm-up cache, then this task is estimated to not end at 1:30. At this time, I need to write an php script to run in the system in the form of daemon, and automatically launch it after the end.

In Linux, there are about three ways to realize script background:

1. Add 1 after the command & Symbols, such as php task. php & The disadvantage of this method is that if the terminal terminal is closed, the php process will be closed with the terminal closed, whether it is normally closed or not. Secondly, if there is output text such as echo or print_r in the code, it will be output to the current terminal window.

2. Use the nohup command, such as nohup php task. php & . By default, The output text such as echo or print_r in the code will be output to the nohup. out file in the directory of the php code. If you close the terminal by normal means such as exit command or close button, The process will not be shut down and will continue to run in the background. However, if the terminal exits or terminates abnormally, the php process will exit immediately. In essence, it is not a stable and reliable daemon scheme.

3. Use fork and setsid, which I will call for the time being: * nix solution. See the code for details:


<?php
 // 1 Times fork 
 $pid = pcntl_fork();
 if ( $pid < 0 ) {
  exit( ' fork error. ' );
 } else if( $pid > 0 ) {
  exit( ' parent process. ' );
 }
 //  Promotes the current child process to the session group leader   This is of vital importance 1 Step  
 if ( ! posix_setsid() ) {
  exit( ' setsid error. ' );
 }
 // 2 Times fork
 $pid = pcntl_fork();
 if( $pid < 0 ){
  exit( ' fork error. ' );
 } else if( $pid > 0 ) {
  exit( ' parent process. ' );
 }
 //  Real logic codes   Let's just write a loop as an example 
 for( $i = 1 ; $i <= 100 ; $i++ ){
  sleep( 1 );
  file_put_contents( 'daemon.log', $i, FILE_APPEND );
 }
?>

Summarize


Related articles: