When launched in daemon mode, attach to the socket

This commit is contained in:
nemunaire 2017-06-23 22:15:26 +02:00
parent b7e12037de
commit 838b76081d
2 changed files with 14 additions and 2 deletions

View File

@ -106,13 +106,25 @@ def attach(pid, socketfile):
return 0 return 0
def daemonize(): def daemonize(socketfile=None):
"""Detach the running process to run as a daemon """Detach the running process to run as a daemon
""" """
import os import os
import sys import sys
if socketfile is not None:
try:
pid = os.fork()
if pid > 0:
import time
os.waitpid(pid, 0)
time.sleep(1)
sys.exit(attach(pid, socketfile))
except OSError as err:
sys.stderr.write("Unable to fork: %s\n" % err)
sys.exit(1)
try: try:
pid = os.fork() pid = os.fork()
if pid > 0: if pid > 0:

View File

@ -148,7 +148,7 @@ def main():
# Daemonize # Daemonize
if not args.debug: if not args.debug:
from nemubot import daemonize from nemubot import daemonize
daemonize() daemonize(args.socketfile)
# Signals handling # Signals handling
def sigtermhandler(signum, frame): def sigtermhandler(signum, frame):