Exemple #1
0
if __name__ == "__main__":
    pname = 'KKB Monitor Agent'
    setproctitle(pname)
    agentPid = os.getpid()
    try:
        pid = kkbLib.readPid('agent')
    except IOError:
        pid = 0

    if pid > 0:
        isRun = kkbLib.checkPidLinux(pid)
        if isRun:
            kkbLib.printout('Program has been started, the process PID:' + str(pid))
            sys.exit(1)

    kkbLib.writePid(agentPid, 'agent')

    kkbLib.info('Starting agent process')
    kkbLib.printout('Starting agent process')
    parentPid = None
    try:
        if len(sys.argv) > 1:
            parentPid = sys.argv[1]
    except Exception:
        kkbLib.error(traceback.format_exc())

    try:
        agentProcess = AgentProcess()
        agentProcess.setName('AgentProcess')
        agentProcess.start()
        sys.stdout.flush()
        sys.stderr.flush()
        si = file(self.stdin, 'r')
        so = file(self.stdout, 'a+')
        se = file(self.stderr, 'a+', 0)
        #重定向标准输入/输出/错误
        os.dup2(si.fileno(), sys.stdin.fileno())
        os.dup2(so.fileno(), sys.stdout.fileno())
        os.dup2(se.fileno(), sys.stderr.fileno())

        #注册程序退出时的函数,即删掉pid文件
        atexit.register(self.delpid)
        pid = str(os.getpid())
        sys.stderr.write("dkdkkfkfkdkdk " + pid + "\n")
        kkbLib.writePid(pid, 'master')

    def delpid(self):
        kkbLib.rmPid('agent')
        kkbLib.rmPid('master')

    def start(self):
        """
        Start the daemon
        """
        # Check for a pidfile to see if the daemon already runs
        try:
            pid = kkbLib.readPid('master')
        except IOError:
            pid = 0