New CLI argument: --pidfile, path to store the daemon PID
This commit is contained in:
parent
f160411f71
commit
150d069dfb
@ -38,6 +38,11 @@ def requires_version(min=None, max=None):
|
|||||||
"but this is nemubot v%s." % (str(max), __version__))
|
"but this is nemubot v%s." % (str(max), __version__))
|
||||||
|
|
||||||
|
|
||||||
|
def attach(pid):
|
||||||
|
print("TODO, attach to %d" % pid)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def daemonize():
|
def daemonize():
|
||||||
"""Detach the running process to run as a daemon
|
"""Detach the running process to run as a daemon
|
||||||
"""
|
"""
|
||||||
|
@ -40,6 +40,9 @@ def main():
|
|||||||
parser.add_argument("-d", "--debug", action="store_true",
|
parser.add_argument("-d", "--debug", action="store_true",
|
||||||
help="don't deamonize, keep in foreground")
|
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",
|
parser.add_argument("-l", "--logfile", default="./nemubot.log",
|
||||||
help="Path to store logs")
|
help="Path to store logs")
|
||||||
|
|
||||||
@ -62,15 +65,33 @@ def main():
|
|||||||
|
|
||||||
# Resolve relatives paths
|
# Resolve relatives paths
|
||||||
args.data_path = os.path.abspath(os.path.expanduser(args.data_path))
|
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.logfile = os.path.abspath(os.path.expanduser(args.logfile))
|
||||||
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)]
|
||||||
|
|
||||||
|
# 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
|
# Daemonize
|
||||||
if not args.debug:
|
if not args.debug:
|
||||||
from nemubot import daemonize
|
from nemubot import daemonize
|
||||||
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
|
# Setup loggin interface
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger("nemubot")
|
logger = logging.getLogger("nemubot")
|
||||||
|
Loading…
Reference in New Issue
Block a user