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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<?php
umask(0);
$pid = pcntl_fork();
if($pid>0){
exit(0);
}
if(-1 == posix_setsid()){
echo 'set sid error';
}
$pid = pcntl_fork();
if($pid > 0){
// 会话首进程退出
exit(0);
}
// 该进程会变成组长进程,会话首进程,没有控制终端
chdir("/");
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
// 当关闭以上标准输出后,如果后面创建文件时,文件描述符会从0开始,一般重定向到 /dev/null代替原来的0,1,2
$stdin = fopen("/dev/null", "a"); //此时$stdin文件描述符为0
$stdout = fopen("/dev/null", "a"); //此时$stdout文件描述符为1
$stderr = fopen("/dev/null", "a"); //此时$stderr文件描述符为2
//file_put_contents("/home/caoayu/test.log", posix_getpid());
$fd = fopen("/home/caoayu/test.log", 'a');
$pid = pcntl_fork();
if($pid == 0){
fprintf($fd, "pid = %d,ppid = %d, sid = %d,time=%s\n",posix_getpid(),posix_getppid(),posix_getsid(posix_getpid()),time());
while (true){
sleep(3);
}
exit(0);
}
$pid = pcntl_wait($status);
if($pid > 0){
fprintf($fd, "exit pid = %d,ppid = %d, sid = %d,time=%s\n",$pid,posix_getppid(),posix_getsid(posix_getpid()),time());
fclose($fd);
exit(0);
}
|