mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
make daemonizing better
This commit is contained in:
parent
82ed62d35d
commit
6f4db28710
@ -7,6 +7,7 @@
|
||||
import logging
|
||||
import optparse as op
|
||||
import os
|
||||
import resource
|
||||
import sys
|
||||
|
||||
from gunicorn.arbiter import Arbiter
|
||||
@ -19,6 +20,14 @@ LOG_LEVELS = {
|
||||
"debug": logging.DEBUG
|
||||
}
|
||||
|
||||
UMASK = 0
|
||||
MAXFD = 1024
|
||||
if (hasattr(os, "devnull")):
|
||||
REDIRECT_TO = os.devnull
|
||||
else:
|
||||
REDIRECT_TO = "/dev/null"
|
||||
|
||||
|
||||
def options():
|
||||
return [
|
||||
op.make_option('--host', dest='host',
|
||||
@ -57,10 +66,32 @@ def configure_logging(opts):
|
||||
|
||||
def daemonize(logger):
|
||||
if not 'GUNICORN_FD' in os.environ:
|
||||
if os.fork(): os._exit(0)
|
||||
os.setsid()
|
||||
if os.fork(): os._exit(0)
|
||||
sys.stdin = sys.__stdin__ = open("/dev/null")
|
||||
if os.fork() == 0:
|
||||
os.setsid()
|
||||
if os.fork() == 0:
|
||||
os.umask(UMASK)
|
||||
else:
|
||||
os._exit(0)
|
||||
else:
|
||||
os._exit(0)
|
||||
|
||||
import resource # Resource usage information.
|
||||
|
||||
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
|
||||
if (maxfd == resource.RLIM_INFINITY):
|
||||
maxfd = 1024
|
||||
|
||||
# Iterate through and close all file descriptors.
|
||||
for fd in range(0, maxfd):
|
||||
try:
|
||||
os.close(fd)
|
||||
except OSError: # ERROR, fd wasn't open to begin with (ignored)
|
||||
pass
|
||||
|
||||
|
||||
os.open(REDIRECT_TO, os.O_RDWR)
|
||||
os.dup2(0, 1)
|
||||
os.dup2(0, 2)
|
||||
|
||||
def main(usage, get_app):
|
||||
parser = op.OptionParser(usage=usage, option_list=options())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user