Add foreground running.

master
q3k 2013-04-08 15:47:36 +02:00
parent c4ad992c50
commit 7cbe3329fe
2 changed files with 28 additions and 25 deletions

View File

@ -16,6 +16,7 @@ def _get_process_list():
def _start_pppd(): def _start_pppd():
print " ".join(config.PPPD)
p = subprocess.Popen(config.PPPD, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p = subprocess.Popen(config.PPPD, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
if p.returncode != 0: if p.returncode != 0:
@ -27,7 +28,7 @@ def _start_pppd():
def _get_pppd_pid(): def _get_pppd_pid():
for process, pid in _get_process_list(): for pid, process in _get_process_list():
if process.startswith("pppd"): if process.startswith("pppd"):
return pid return pid

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
import os import os
import time
import sys import sys
import argparse import argparse
@ -67,35 +66,38 @@ def doublefork(pidfile, logfile):
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Make sure the ADSL network is up and display info on the FWA fron panel.") parser = argparse.ArgumentParser(description="Make sure the ADSL network is up and display info on the FWA fron panel.")
parser.add_argument("--pidfile", default="/var/run/adsl-bouncer.pid", help="Path of file to write dameon PID to.") parser.add_argument("--daemonize", default="true", help="Whether we should daemonize the bouncer.")
parser.add_argument("--logfile", default="/var/log/adsl-bouncer.log", help="Path of log file.") parser.add_argument("--pidfile", default="/var/run/adsl-bouncer.pid", help="When daemonized, the path of file to write dameon PID to.")
parser.add_argument("--logfile", default="/var/log/adsl-bouncer.log", help="When daemonized, the path of log file.")
args = parser.parse_args() args = parser.parse_args()
# see if daemon is already running daemonize = True if args.daemonize.lower() in ["true", "t", "yes", "y"] else False
if os.path.exists(args.pidfile): if daemonize:
with open(args.pidfile, "r") as f: # see if daemon is already running
try: if os.path.exists(args.pidfile):
pid = int(f.read()) with open(args.pidfile, "r") as f:
except:
pass
else:
try: try:
os.kill(pid, 0) pid = int(f.read())
except OSError: except:
pass pass
else: else:
# daemon is running try:
raise Exception("Daemon is already running on PID %i!" % pid) os.kill(pid, 0)
# try to open logfile except OSError:
try: pass
f = open(args.logfile, "aw") else:
f.write("Daemon starting...\n") # daemon is running
f.close() raise Exception("Daemon is already running on PID %i!" % pid)
except IOError as e: # try to open logfile
raise Exception("Could not open logfile %s! %s" % (args.logfile, str(e))) try:
f = open(args.logfile, "aw")
f.write("Daemon starting...\n")
f.close()
except IOError as e:
raise Exception("Could not open logfile %s! %s" % (args.logfile, str(e)))
if not doublefork(args.pidfile, args.logfile): if not doublefork(args.pidfile, args.logfile):
raise Exception("Could not doublefork! See logfile for details.") raise Exception("Could not doublefork! See logfile for details.")
sys.stderr.write("Daemon running! PID %i.\n" % os.getpid()) sys.stderr.write("Daemon running! PID %i.\n" % os.getpid())
adsl.loop() adsl.loop()