Extract deamonize to a dedicated function that can be called from anywhere
This commit is contained in:
parent
57275f5735
commit
b0678ceb84
|
@ -38,6 +38,44 @@ def requires_version(min=None, max=None):
|
||||||
"but this is nemubot v%s." % (str(max), __version__))
|
"but this is nemubot v%s." % (str(max), __version__))
|
||||||
|
|
||||||
|
|
||||||
|
def daemonize():
|
||||||
|
"""Detach the running process to run as a daemon
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
try:
|
||||||
|
pid = os.fork()
|
||||||
|
if pid > 0:
|
||||||
|
sys.exit(0)
|
||||||
|
except OSError as err:
|
||||||
|
sys.stderr.write("Unable to fork: %s\n" % err)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
os.setsid()
|
||||||
|
os.umask(0)
|
||||||
|
os.chdir('/')
|
||||||
|
|
||||||
|
try:
|
||||||
|
pid = os.fork()
|
||||||
|
if pid > 0:
|
||||||
|
sys.exit(0)
|
||||||
|
except OSError as err:
|
||||||
|
sys.stderr.write("Unable to fork: %s\n" % err)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
sys.stdout.flush()
|
||||||
|
sys.stderr.flush()
|
||||||
|
si = open(os.devnull, 'r')
|
||||||
|
so = open(os.devnull, 'a+')
|
||||||
|
se = open(os.devnull, 'a+')
|
||||||
|
|
||||||
|
os.dup2(si.fileno(), sys.stdin.fileno())
|
||||||
|
os.dup2(so.fileno(), sys.stdout.fileno())
|
||||||
|
os.dup2(se.fileno(), sys.stderr.fileno())
|
||||||
|
|
||||||
|
|
||||||
def reload():
|
def reload():
|
||||||
"""Reload code of all Python modules used by nemubot
|
"""Reload code of all Python modules used by nemubot
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -68,35 +68,8 @@ def main():
|
||||||
|
|
||||||
# Daemonize
|
# Daemonize
|
||||||
if not args.debug:
|
if not args.debug:
|
||||||
try:
|
from nemubot import daemonize
|
||||||
pid = os.fork()
|
daemonize()
|
||||||
if pid > 0:
|
|
||||||
sys.exit(0)
|
|
||||||
except OSError as err:
|
|
||||||
sys.stderr.write("Unable to fork: %s" % err)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
os.setsid()
|
|
||||||
os.umask(0)
|
|
||||||
os.chdir('/')
|
|
||||||
|
|
||||||
try:
|
|
||||||
pid = os.fork()
|
|
||||||
if pid > 0:
|
|
||||||
sys.exit(0)
|
|
||||||
except OSError as err:
|
|
||||||
sys.stderr.write("Unable to fork: %s" % err)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
sys.stdout.flush()
|
|
||||||
sys.stderr.flush()
|
|
||||||
si = open(os.devnull, 'r')
|
|
||||||
so = open(os.devnull, 'a+')
|
|
||||||
se = open(os.devnull, 'a+')
|
|
||||||
|
|
||||||
os.dup2(si.fileno(), sys.stdin.fileno())
|
|
||||||
os.dup2(so.fileno(), sys.stdout.fileno())
|
|
||||||
os.dup2(se.fileno(), sys.stderr.fileno())
|
|
||||||
|
|
||||||
# Setup loggin interface
|
# Setup loggin interface
|
||||||
import logging
|
import logging
|
||||||
|
|
Loading…
Reference in New Issue
Block a user