New CLI argument: --pidfile, path to store the daemon PID

This commit is contained in:
nemunaire 2015-05-18 07:36:49 +02:00
parent e17368cf26
commit 0b14207c88
2 changed files with 26 additions and 0 deletions

View File

@ -38,6 +38,11 @@ def requires_version(min=None, max=None):
"but this is nemubot v%s." % (str(max), __version__))
def attach(pid):
print("TODO, attach to %d" % pid)
return 0
def daemonize():
"""Detach the running process to run as a daemon
"""

View File

@ -40,6 +40,9 @@ def main():
parser.add_argument("-d", "--debug", action="store_true",
help="don't deamonize, keep in foreground")
parser.add_argument("-P", "--pidfile", default="./nemubot.pid",
help="Path to the file where store PID")
parser.add_argument("-l", "--logfile", default="./nemubot.log",
help="Path to store logs")
@ -62,15 +65,33 @@ def main():
# Resolve relatives paths
args.data_path = os.path.abspath(os.path.expanduser(args.data_path))
args.pidfile = os.path.abspath(os.path.expanduser(args.pidfile))
args.logfile = os.path.abspath(os.path.expanduser(args.logfile))
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)]
# Check if an instance is already launched
if args.pidfile is not None and os.path.isfile(args.pidfile):
with open(args.pidfile, "r") as f:
pid = int(f.readline())
try:
os.kill(pid, 0)
except OSError:
pass
else:
from nemubot import attach
sys.exit(attach(pid))
# Daemonize
if not args.debug:
from nemubot import daemonize
daemonize()
# Store PID to pidfile
if args.pidfile is not None:
with open(args.pidfile, "w+") as f:
f.write(str(os.getpid()))
# Setup loggin interface
import logging
logger = logging.getLogger("nemubot")