From 282f842663f5f72b59637299615427cd2a8c29f6 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Tue, 17 Jun 2008 22:46:55 +0000 Subject: [PATCH] Modify startup script to make a true unix daemon by using two fork()s Work will need to be done to make deluged a windows service --- deluge/core/daemon.py | 8 +++++++ deluge/main.py | 51 +++++++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/deluge/core/daemon.py b/deluge/core/daemon.py index 77be90adb..e1196ad09 100644 --- a/deluge/core/daemon.py +++ b/deluge/core/daemon.py @@ -32,10 +32,18 @@ # statement from all source files in the program, then also delete it here. import deluge.configmanager +import deluge.common from deluge.log import LOG as log class Daemon: def __init__(self, options, args): + version = deluge.common.get_version() + if deluge.common.get_revision() != "": + version = version + "r" + deluge.common.get_revision() + + log.info("Deluge daemon %s", version) + log.debug("options: %s", options) + log.debug("args: %s", args) # Set the config directory deluge.configmanager.set_config_dir(options.config) diff --git a/deluge/main.py b/deluge/main.py index 7bd32329a..b01141793 100644 --- a/deluge/main.py +++ b/deluge/main.py @@ -37,6 +37,8 @@ """Main starting point for Deluge. Contains the main() entry point.""" import os +import os.path +import sys from optparse import OptionParser import deluge.common @@ -71,6 +73,8 @@ def start_ui(): def start_daemon(): """Entry point for daemon script""" + import deluge.common + # Setup the argument parser parser = OptionParser(usage="%prog [options] [actions]", version=deluge.common.get_version()) @@ -80,29 +84,34 @@ def start_daemon(): help="Do not daemonize", action="store_true", default=False) parser.add_option("-c", "--config", dest="config", help="Set the config location", action="store", type="str") + parser.add_option("-l", "--logfile", dest="logfile", + help="Set the logfile location", action="store", type="str") # Get the options and args from the OptionParser (options, args) = parser.parse_args() - from deluge.log import LOG as log - - version = deluge.common.get_version() - if deluge.common.get_revision() != "": - version = version + "r" + deluge.common.get_revision() - - log.info("Deluge daemon %s", version) - log.debug("options: %s", options) - log.debug("args: %s", args) - - from deluge.core.daemon import Daemon + # If the donot daemonize is set, then we just skip the forking + if not options.donot: + if os.fork() == 0: + os.setsid() + if os.fork() == 0: + if options.logfile: + logfile = options.logfile + else: + if options.config: + logfile = os.path.join(options.config, "deluged.log") + else: + config_dir = deluge.common.get_default_config_dir() + logfile = os.path.join(config_dir, "deluged.log") + + sys.stdout = open(logfile, "w") + sys.stderr = sys.stdout + sys.stdin = None + else: + os._exit(0) + else: + os._exit(0) - if options.donot: - log.info("Starting daemon..") - Daemon(options, args) - else: - cmd = "deluged -d " + "".join(a for a in args) - if options.port != None: - cmd = cmd + " -p %s" % options.port - if options.config != None: - cmd = cmd + " -c %s" % options.config - os.popen2(cmd) + from deluge.core.daemon import Daemon + Daemon(options, args) +