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
def daemonize():
def daemonize(socketfile=None):
"""Detach the running process to run as a daemon
"""
import os
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:
pid = os.fork()
if pid > 0:

View File

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