Introducing daemon mode
This commit is contained in:
parent
91b550754f
commit
992c847e27
@ -36,6 +36,9 @@ def main():
|
|||||||
default=["./modules/"],
|
default=["./modules/"],
|
||||||
help="directory to use as modules store")
|
help="directory to use as modules store")
|
||||||
|
|
||||||
|
parser.add_argument("-d", "--debug", action="store_true",
|
||||||
|
help="don't deamonize, keep in foreground")
|
||||||
|
|
||||||
parser.add_argument("-l", "--logfile", default="./nemubot.log",
|
parser.add_argument("-l", "--logfile", default="./nemubot.log",
|
||||||
help="Path to store logs")
|
help="Path to store logs")
|
||||||
|
|
||||||
@ -62,6 +65,38 @@ def main():
|
|||||||
args.files = [ x for x in map(os.path.abspath, args.files)]
|
args.files = [ x for x in map(os.path.abspath, args.files)]
|
||||||
args.modules_path = [ x for x in map(os.path.abspath, args.modules_path)]
|
args.modules_path = [ x for x in map(os.path.abspath, args.modules_path)]
|
||||||
|
|
||||||
|
# Daemonize
|
||||||
|
if not args.debug:
|
||||||
|
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)
|
||||||
|
|
||||||
|
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
|
||||||
logger = logging.getLogger("nemubot")
|
logger = logging.getLogger("nemubot")
|
||||||
@ -70,6 +105,7 @@ def main():
|
|||||||
formatter = logging.Formatter(
|
formatter = logging.Formatter(
|
||||||
'%(asctime)s %(name)s %(levelname)s %(message)s')
|
'%(asctime)s %(name)s %(levelname)s %(message)s')
|
||||||
|
|
||||||
|
if args.debug:
|
||||||
ch = logging.StreamHandler()
|
ch = logging.StreamHandler()
|
||||||
ch.setFormatter(formatter)
|
ch.setFormatter(formatter)
|
||||||
if args.verbose < 2:
|
if args.verbose < 2:
|
||||||
@ -146,7 +182,7 @@ def main():
|
|||||||
"the prompt.")
|
"the prompt.")
|
||||||
|
|
||||||
context.quit()
|
context.quit()
|
||||||
print("Waiting for other threads shuts down...")
|
logger.info("Waiting for other threads shuts down...")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user