-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngx_daemon.php
55 lines (45 loc) · 1.24 KB
/
ngx_daemon.php
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
46
47
48
49
50
51
52
53
54
<?php
/**
* Created by PhpStorm.
* User: zenus
* Date: 15-12-13
* Time: 下午7:43
* @param ngx_log $log
* @return int
*/
function ngx_daemon(ngx_log $log)
{
//int fd;
switch (pcntl_fork()) {
case -1:
ngx_log_error(NGX_LOG_EMERG, $log, pcntl_get_last_error(), "fork() failed");
return NGX_ERROR;
case 0:
break;
default:
exit(0);
}
ngx_pid(ngx_getpid());
if (posix_setsid() == -1) {
ngx_log_error(NGX_LOG_EMERG, $log, posix_get_last_error(), "setsid() failed");
return NGX_ERROR;
}
umask(0);
//todo instead of redirect to /dev/null, i simple close them;
if (fclose(STDIN) == false) {
ngx_log_error(NGX_LOG_EMERG, $log, NGX_FERROR, "close stdin failed");
return NGX_ERROR;
}
if (fclose(STDOUT) == false) {
ngx_log_error(NGX_LOG_EMERG, $log, NGX_FERROR, "close stdout failed");
return NGX_ERROR;
}
if (fclose(STDERR) == false) {
ngx_log_error(NGX_LOG_EMERG, $log, NGX_FERROR, "close stderr failed");
return NGX_ERROR;
}
return NGX_OK;
// $STDIN = fopen('/dev/null', 'r');
// $STDOUT = fopen('/dev/null', 'wb');
// $STDERR = fopen('/dev/null', 'wb');
}