main: new option -A to run as daemon

This commit is contained in:
nemunaire 2017-07-15 10:53:30 +02:00
parent 5f89428562
commit 1db63600e5
2 changed files with 12 additions and 6 deletions

View File

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

View File

@ -37,6 +37,9 @@ def main():
default=["./modules/"],
help="directory to use as modules store")
parser.add_argument("-A", "--no-attach", action="store_true",
help="don't attach after fork")
parser.add_argument("-d", "--debug", action="store_true",
help="don't deamonize, keep in foreground")
@ -148,7 +151,7 @@ def main():
# Daemonize
if not args.debug:
from nemubot import daemonize
daemonize(args.socketfile)
daemonize(args.socketfile, not args.no_attach)
# Signals handling
def sigtermhandler(signum, frame):