Handle daemons for dnsmasq

This commit is contained in:
Joshua Boniface 2018-10-03 23:16:46 -04:00
parent 717d1a9045
commit 3e5e50e438
1 changed files with 32 additions and 5 deletions

View File

@ -22,23 +22,50 @@
import subprocess import subprocess
import threading import threading
import signal
def run_os_command(command_string, background=False): import daemon_lib.ansiiprint as ansiiprint
class OSDaemon(object):
def __init__(self, command, environment):
self.proc = subprocess.Popen(
command,
env=environment,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
def signal(self, signal):
signal_map = {
'hup': signal.SIGHUP,
'int': signal.SIGINT
}
self.proc.send_signal(signal_map[signal])
def run_os_daemon(command_string, background=False, environment=None, return_pid=False):
command = command_string.split()
daemon = OSDaemon(command, environment)
return daemon
# Run a oneshot command, optionally without blocking
def run_os_command(command_string, background=False, environment=None):
command = command_string.split() command = command_string.split()
if background: if background:
def runcmd(): def runcmd():
subprocess.run( subprocess.Popen(
command, command,
env=environment,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE stderr=subprocess.PIPE,
) )
thread = threading.Thread(target=runcmd, args=()) thread = threading.Thread(target=runcmd, args=())
thread.start() thread.start()
return 0 return 0
else: else:
command_output = subprocess.run( command_output = subprocess.Popen(
command, command,
env=environment,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE stderr=subprocess.PIPE,
) )
return command_output.returncode return command_output.returncode