Added support for windows service

Added support for running as a windows service so it does not fork the process on reboot.
This commit is contained in:
Vashypooh 2017-07-22 16:02:42 -04:00
parent af77e51307
commit b3fe6145e2
2 changed files with 13 additions and 4 deletions

View file

@ -92,6 +92,8 @@ def main():
'--nolaunch', action='store_true', help='Prevent browser from launching on startup')
parser.add_argument(
'--pidfile', help='Create a pid file (only relevant when running as a daemon)')
parser.add_argument(
'--windowsservice', action='store_true', help='Running as a windows service.')
args = parser.parse_args()
@ -116,6 +118,10 @@ def main():
plexpy.DAEMON = True
plexpy.QUIET = True
if args.windowsservice:
plexpy.WINDOWSSERVICE = True
logger.info("Running as windows service: %s", plexpy.WINDOWSSERVICE)
if args.pidfile:
plexpy.PIDFILE = str(args.pidfile)

View file

@ -54,6 +54,7 @@ VERBOSE = True
DAEMON = False
CREATEPID = False
PIDFILE = None
WINDOWSSERVICE = False
SCHED = BackgroundScheduler()
SCHED_LOCK = threading.Lock()
@ -989,15 +990,17 @@ def shutdown(restart=False, update=False):
args += ARGS
if '--nolaunch' not in args:
args += ['--nolaunch']
logger.info('Restarting PlexPy with %s', args)
# os.execv fails with spaced names on Windows
# https://bugs.python.org/issue19066
if os.name == 'nt':
if os.name == 'nt' and plexpy.WINDOWSSERVICE:
logger.info("Running as windows service, no need to fork.")
elif os.name == 'nt':
subprocess.Popen(args, cwd=os.getcwd())
logger.info('Restarting PlexPy with %s', args)
else:
os.execv(exe, args)
logger.info('Restarting PlexPy with %s', args)
os._exit(0)