Support logging of daemon directly to a file

This commit is contained in:
Joshua Boniface 2018-10-15 20:54:49 -04:00
parent a5c76c5d41
commit a2a7a1d790
1 changed files with 14 additions and 6 deletions

View File

@ -29,14 +29,23 @@ import time
import pvcd.log as log import pvcd.log as log
class OSDaemon(object): class OSDaemon(object):
def __init__(self, command, environment): def __init__(self, command_string, environment, logfile):
command = command_string.split()
# Set stdout to be a logfile if set
if logfile:
stdout = open(logfile, 'a')
else:
stdout = subprocess.PIPE
# Invoke the process
self.proc = subprocess.Popen( self.proc = subprocess.Popen(
command, command,
env=environment, env=environment,
stdout=subprocess.PIPE, stdout=stdout,
stderr=subprocess.PIPE, stderr=stdout,
) )
# Signal the process
def signal(self, sent_signal): def signal(self, sent_signal):
signal_map = { signal_map = {
'hup': signal.SIGHUP, 'hup': signal.SIGHUP,
@ -45,9 +54,8 @@ class OSDaemon(object):
} }
self.proc.send_signal(signal_map[sent_signal]) self.proc.send_signal(signal_map[sent_signal])
def run_os_daemon(command_string, environment=None): def run_os_daemon(command_string, environment=None, logfile=None):
command = command_string.split() daemon = OSDaemon(command_string, environment, logfile)
daemon = OSDaemon(command, environment)
return daemon return daemon
# Run a oneshot command, optionally without blocking # Run a oneshot command, optionally without blocking