Add -P, --pidfile option to deluged

This commit is contained in:
Andrew Resch 2008-11-26 07:01:19 +00:00
parent b141d91965
commit 2969ff51f9
2 changed files with 45 additions and 30 deletions

View File

@ -33,6 +33,9 @@ Sets the configuration path.
.TP .TP
.I -l LOGFILE, --logfile=LOGFILE .I -l LOGFILE, --logfile=LOGFILE
Output to designated logfile instead of stdout Output to designated logfile instead of stdout
.TP
.I -P PIDFILE, --pidfile=PIDFILE
Use pidfile to store process id
.SH SEE ALSO .SH SEE ALSO
.B Homepage: .B Homepage:

View File

@ -108,6 +108,8 @@ def start_daemon():
help="Set the config location", action="store", type="str") help="Set the config location", action="store", type="str")
parser.add_option("-l", "--logfile", dest="logfile", parser.add_option("-l", "--logfile", dest="logfile",
help="Set the logfile location", action="store", type="str") help="Set the logfile location", action="store", type="str")
parser.add_option("-P", "--pidfile", dest="pidfile",
help="Use pidfile to store process id", action="store", type="str")
# Get the options and args from the OptionParser # Get the options and args from the OptionParser
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
@ -123,42 +125,52 @@ def start_daemon():
if not os.path.exists(deluge.common.get_default_config_dir()): if not os.path.exists(deluge.common.get_default_config_dir()):
os.makedirs(deluge.common.get_default_config_dir()) os.makedirs(deluge.common.get_default_config_dir())
# Returns a path to the logfile
def open_logfile():
if options.logfile:
return options.logfile
else:
if options.config:
return os.path.join(options.config, "deluged.log")
else:
config_dir = deluge.common.get_default_config_dir()
return os.path.join(config_dir, "deluged.log")
# Writes out a pidfile if necessary
def write_pidfile():
if options.pidfile:
open(options.pidfile, "wb").write("%s\n" % os.getpid())
# If the donot daemonize is set, then we just skip the forking # If the donot daemonize is set, then we just skip the forking
if not options.donot and not deluge.common.windows_check(): if not options.donot:
# Windows check, we log to the config folder by default
if deluge.common.windows_check():
# Open a logfile
sys.stdout = open(open_logfile(), "wb")
sys.stderr = sys.stdout
sys.stdin = None
# Write pidfile
write_pidfile()
else:
if os.fork() == 0: if os.fork() == 0:
os.setsid() os.setsid()
if os.fork() == 0: if os.fork() == 0:
if options.logfile: # Open a logfile
logfile = options.logfile sys.stdout = open(open_logfile(), "wb")
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, "wb")
sys.stderr = sys.stdout sys.stderr = sys.stdout
sys.stdin = None sys.stdin = None
# Write pidfile
write_pidfile()
else: else:
os._exit(0) os._exit(0)
else: else:
os._exit(0) os._exit(0)
# Windows check, we log to the config folder by default
if not options.donot and deluge.common.windows_check():
if options.logfile:
logfile = options.logfile
else: else:
if options.config: # Do not daemonize
logfile = os.path.join(options.config, "deluged.log") write_pidfile()
else:
config_dir = deluge.common.get_default_config_dir()
logfile = os.path.join(config_dir, "deluged.log")
sys.stdout = open(logfile, "wb")
sys.stderr = sys.stdout
sys.stdin = None
from deluge.core.daemon import Daemon from deluge.core.daemon import Daemon
Daemon(options, args) Daemon(options, args)