Home  >  Article  >  Backend Development  >  PHP extension implements daemon process_PHP tutorial

PHP extension implements daemon process_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:57:04896browse

PHP extension program implements daemon process

Generally, server programs run in the background of the system, which is very different from ordinary interactive command line programs. There is a function daemon in glibc. Calling this function can cause the current process to leave the terminal and become a daemon process. For details, see man daemon. There is currently no such function in PHP. Of course, if you are interested, you can write a PHP extension function to implement it.

There are two ways to implement daemonization of PHP command line programs:

1. Use nohup

Copy the code The code is as follows:


nohup php myprog.php > log.txt &

Daemonization is implemented here.

Execute php myprog.php alone. When ctrl c is pressed, program execution will be interrupted and the current process and child processes will be killed.

php myprog.php &, although the execution program is also run in the background, it actually relies on the terminal. When the user exits the terminal, the process will be killed.

2. Use PHP code to implement

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

function daemonize()

{

$pid = pcntl_fork();

if ($pid == -1)

{

die("fork(1) failed!n");

}

elseif ($pid > 0)

{

//让由用户启动的进程退出

exit(0);

}

 

//建立一个有别于终端的新session以脱离终端

posix_setsid();

 

$pid = pcntl_fork();

if ($pid == -1)

{

die("fork(2) failed!n");

}

elseif ($pid > 0)

{

//父进程退出, 剩下子进程成为最终的独立进程

exit(0);

}

}

 

daemonize();

sleep(1000);

1 2

3

4 5

67 8 9 10 11 12
13
14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
function daemonize() { $pid = pcntl_fork(); if ($pid == -1) { die("fork(1) failed!n"); } elseif ($pid > 0) { //Let the process started by the user exit exit(0); } //Create a new session different from the terminal to break away from the terminal posix_setsid(); $pid = pcntl_fork(); if ($pid == -1) { die("fork(2) failed!n"); } elseif ($pid > 0) { //The parent process exits, and the remaining child process becomes the final independent process exit(0); } } daemonize(); sleep(1000);
Use the above code to implement daemonization. When your PHP program needs to run in the background, you only need to call the encapsulated function daemonize() once. Note: Redirection of standard input and output is not implemented here. http://www.bkjia.com/PHPjc/985143.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/985143.htmlTechArticlePHP extension program implements daemon process. Generally, server programs run in the background of the system, which is different from ordinary interactive command lines. The procedures make a big difference. There is a function daemon in glibc. Call this...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn