From 123cc3bbf981f81a9f23c3d355dd402782cf6472 Mon Sep 17 00:00:00 2001 From: ShyPike Date: Fri, 6 May 2011 22:30:13 +0200 Subject: [PATCH] Implement PID file support for non-Windows systems. Triggered by command line parameter: --pid --- SABnzbd.py | 12 +++++++++++- sabnzbd/__init__.py | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/SABnzbd.py b/SABnzbd.py index 391f759..7fdc62a 100755 --- a/SABnzbd.py +++ b/SABnzbd.py @@ -237,6 +237,7 @@ def print_help(): print " -d --daemon Use when run as a service" else: print " -d --daemon Fork daemon process" + print " --pid Create a PID file in the listed folder (full path)" print print " --force Discard web-port timeout (see Wiki!)" print " -h --help Print this message" @@ -760,7 +761,7 @@ def commandline_handler(frozen=True): 'weblogging=', 'server=', 'templates', 'template2', 'browser=', 'config-file=', 'force', 'version', 'https=', 'autorestarted', 'repair', 'repair-all', - 'log-all', 'no-login', + 'log-all', 'no-login', 'pid=', # Below Win32 Service options 'password=', 'username=', 'startup=', 'perfmonini=', 'perfmondll=', 'interactive', 'wait=', @@ -830,6 +831,7 @@ def main(): api_url = None no_login = False re_argv = [sys.argv[0]] + pid_path = None service, sab_opts, serv_opts, upload_nzbs = commandline_handler() @@ -904,6 +906,10 @@ def main(): sabnzbd.LOG_ALL = True elif opt in ('--no-login',): no_login = True + elif opt in ('--pid',): + pid_path = arg + re_argv.append(opt) + re_argv.append(arg) sabnzbd.MY_FULLNAME = os.path.normpath(os.path.abspath(sabnzbd.MY_FULLNAME)) sabnzbd.MY_NAME = os.path.basename(sabnzbd.MY_FULLNAME) @@ -1384,6 +1390,9 @@ def main(): # Write URL directly to registry set_connection_info(api_url) + if pid_path: + sabnzbd.pid_file(pid_path, cherryport) + # Have to keep this running, otherwise logging will terminate timer = 0 while not sabnzbd.SABSTOP: @@ -1477,6 +1486,7 @@ def main(): logging.info('Leaving SABnzbd') sys.stderr.flush() sys.stdout.flush() + sabnzbd.pid_file() if getattr(sys, 'frozen', None) == 'macosx_app': AppHelper.stopEventLoop() else: diff --git a/sabnzbd/__init__.py b/sabnzbd/__init__.py index 7ac1b83..aee1840 100644 --- a/sabnzbd/__init__.py +++ b/sabnzbd/__init__.py @@ -98,6 +98,7 @@ DIR_LCLDATA = None DIR_PROG = None DIR_INTERFACES = None DIR_LANGUAGE = None +DIR_PID = None QUEUECOMPLETE = None #stores the nice name of the action QUEUECOMPLETEACTION = None #stores the name of the function to be called @@ -160,6 +161,8 @@ def sig_handler(signum = None, frame = None): if sabnzbd.WIN32: from util.apireg import del_connection_info del_connection_info() + else: + pid_file() SABSTOP = True os._exit(0) @@ -963,6 +966,26 @@ def check_all_tasks(): return True +def pid_file(pid_path=None, port=0): + """ Create or remove pid file + """ + global DIR_PID + if not sabnzbd.WIN32 and pid_path and pid_path.startswith('/'): + DIR_PID = os.path.join(pid_path, 'sabnzbd-%s.pid' % port) + + if DIR_PID: + try: + if port: + f = open(DIR_PID, 'w') + f.write('%d\n' % os.getpid()) + f.close() + else: + os.remove(DIR_PID) + except: + logging.warning('Cannot access PID file %s', DIR_PID) + + + # Required wrapper because nzbstuff.py cannot import downloader.py def active_primaries(): return sabnzbd.downloader.Downloader.do.active_primaries()