Add foreground running.
parent
c4ad992c50
commit
7cbe3329fe
3
adsl.py
3
adsl.py
|
@ -16,6 +16,7 @@ def _get_process_list():
|
|||
|
||||
|
||||
def _start_pppd():
|
||||
print " ".join(config.PPPD)
|
||||
p = subprocess.Popen(config.PPPD, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout, stderr = p.communicate()
|
||||
if p.returncode != 0:
|
||||
|
@ -27,7 +28,7 @@ def _start_pppd():
|
|||
|
||||
|
||||
def _get_pppd_pid():
|
||||
for process, pid in _get_process_list():
|
||||
for pid, process in _get_process_list():
|
||||
if process.startswith("pppd"):
|
||||
return pid
|
||||
|
||||
|
|
50
bouncer.py
50
bouncer.py
|
@ -1,7 +1,6 @@
|
|||
#!/usr/bin/env python2
|
||||
|
||||
import os
|
||||
import time
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
|
@ -67,35 +66,38 @@ def doublefork(pidfile, logfile):
|
|||
|
||||
if __name__ == "__main__":
|
||||
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("--logfile", default="/var/log/adsl-bouncer.log", help="Path of log file.")
|
||||
parser.add_argument("--daemonize", default="true", help="Whether we should daemonize the bouncer.")
|
||||
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()
|
||||
# see if daemon is already running
|
||||
if os.path.exists(args.pidfile):
|
||||
with open(args.pidfile, "r") as f:
|
||||
try:
|
||||
pid = int(f.read())
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
daemonize = True if args.daemonize.lower() in ["true", "t", "yes", "y"] else False
|
||||
if daemonize:
|
||||
# see if daemon is already running
|
||||
if os.path.exists(args.pidfile):
|
||||
with open(args.pidfile, "r") as f:
|
||||
try:
|
||||
os.kill(pid, 0)
|
||||
except OSError:
|
||||
pid = int(f.read())
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
# daemon is running
|
||||
raise Exception("Daemon is already running on PID %i!" % pid)
|
||||
# try to open logfile
|
||||
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)))
|
||||
try:
|
||||
os.kill(pid, 0)
|
||||
except OSError:
|
||||
pass
|
||||
else:
|
||||
# daemon is running
|
||||
raise Exception("Daemon is already running on PID %i!" % pid)
|
||||
# try to open logfile
|
||||
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):
|
||||
raise Exception("Could not doublefork! See logfile for details.")
|
||||
if not doublefork(args.pidfile, args.logfile):
|
||||
raise Exception("Could not doublefork! See logfile for details.")
|
||||
|
||||
sys.stderr.write("Daemon running! PID %i.\n" % os.getpid())
|
||||
adsl.loop()
|
||||
|
|
Loading…
Reference in New Issue