ㅇ Daemon 모듈
ㅇ main program에서
#!/usr/bin/env python
#
#
#http://homepage.hispeed.ch/py430/python/daemon.py
import sys, os
import logging
from os.path import dirname, isdir, join, basename, exists
f
def daemonize(pid_filename=None):
_basename = basename(pid_filename).split(".")[0]
pid_file = join(PID_DIR, "%s.pid" % _basename)
#if already started, quit quietly.
if exists(pid_file):
f = open(pid_file)
pid = f.read().strip()
f.close()
if isdir("/proc/%s" % pid):
logging.info("process('%s') already started with pid=%s." % (_basename, pid))
sys.exit(0)
wd = dirname(__file__)
os.chdir(wd)
if hasattr(os, "devnull"):
devnull = os.devnull
else:
devnull = "/dev/null"
sys.stdin = open(devnull, "r")
sys.stdout = sys.stderr = open(devnull, "w")
# do the UNIX double-fork magic, see Stevens' "Advanced
# Programming in the UNIX Environment" for details (ISBN 0201563177)
try:
pid = os.fork()
if pid > 0: # exit first parent
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
# decouple from parent environment
#NOTE: if chdir to '/' dirname(__file__) will not work correctly
#os.chdir("/") #don't prevent unmounting....
os.setsid()
os.umask(0)
# do a second fork
try:
pid = os.fork()
if pid > 0:
if pid_file:
if not isdir(PID_DIR):
os.makedirs(PID_DIR)
f = open(pid_file, 'w')
f.write("%d\n" % pid)
f.close()
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
from Daemon import daemonize
#----------------------------------------------------------------------
def main(front):
pid_filename = basename(__file__).split(".")[0]
if not front:
daemonize(pid_filename)
init_signal_handler()
init_log("client")
#create the aliveness lock for the agent server
from OVSConstants import AGENT_LIVENESS_LOCK
if exists(AGENT_LIVENESS_LOCK):
os.remove(AGENT_LIVENESS_LOCK)
fd = os.open(AGENT_LIVENESS_LOCK, os.O_CREAT)
try:
from fcntl import flock, LOCK_EX, LOCK_NB
flock(fd, LOCK_EX | LOCK_NB)
except Exception, e:
logging.error("OVSAgentServer.main(): cannot create lock for agent aliveness! => %s : %s" % (e.__class__, e))
sys.exit(1)
#remove shutdown mode flag
if exists(AGENT_SHUTDOWN_MODE_FLAG):
os.remove(AGENT_SHUTDOWN_MODE_FLAG)
#overide some of the system default modules, using the compat version
execfile(join(dirname(__file__), "OVSCompat.py"))
#to set dom0 weight 512
try:
from OVSCommons import run_cmd
run_cmd(args = ['xm', 'sched-credit', '-d', '0', '-w', '512'])
except:
pass
start_server()
if __name__ == "__main__":
front = False
if len(sys.argv) > 1:
if sys.argv[1] == 'front':
front = True
main(front)