From 1db63600e534ebdf65d28a5aea6b31be883334d7 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Sat, 15 Jul 2017 10:53:30 +0200 Subject: [PATCH] main: new option -A to run as daemon --- nemubot/__init__.py | 13 ++++++++----- nemubot/__main__.py | 5 ++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/nemubot/__init__.py b/nemubot/__init__.py index 4b14c07..48de6ea 100644 --- a/nemubot/__init__.py +++ b/nemubot/__init__.py @@ -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) diff --git a/nemubot/__main__.py b/nemubot/__main__.py index fa9d3ba..e1576fb 100644 --- a/nemubot/__main__.py +++ b/nemubot/__main__.py @@ -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):