def stop(self): """ Stop the daemon """ logger.debug("Stop Daemon...") print "Stop Daemon..." #wait 2s for gracefully shutdown, then will force kill wait_stop = 2 try: pf = file(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if pid and linux.process_exists(pid): self.stop_agent_by_pid(pid, wait_stop) else: message = "pidfile %s does not exist. Daemon not running?\n" sys.stderr.write(message % self.pidfile) pids = linux.get_agent_pid_by_name(self.py_process_name) if not pids: message = "Daemon not running?\n" sys.stderr.write(message) return # not an error in a restart # exclude self pid for pid in pids.split('\n'): if pid and int(pid) != os.getpid(): pid = int(pid) self.stop_agent_by_pid(pid, wait_stop)
def start(self): """ Start the daemon """ print "Start Daemon..." # Check for a pidfile to see if the daemon already runs try: locale.setlocale(locale.LC_ALL, 'C') pf = file(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if pid: if linux.process_exists(pid): message = "Daemon already running, pid is %s\n" sys.stderr.write(message % pid) sys.exit(0) # Start the daemon self.daemonize() try: self.run() except Exception: content = traceback.format_exc() logger.error(content) sys.exit(1) print "Start Daemon Successfully"
def get_start_agent_by_name(self): pids = linux.get_agent_pid_by_name(self.py_process_name) for pid in pids.split('\n'): if pid and int(pid) != os.getpid(): pid = int(pid) if linux.process_exists(pid): message = "Daemon already running, pid is %s\n" sys.stderr.write(message % pid) sys.exit(0)
def stop_agent_by_pid(self, pid, wait_stop): # Try killing the daemon process start_time = time.time() while 1: if linux.process_exists(pid): curr_time = time.time() if (curr_time - start_time) > wait_stop: os.kill(pid, SIGKILL) else: os.kill(pid, SIGTERM) time.sleep(0.3) else: break print "Stop Daemon Successfully"
def start(self): """ Start the daemon """ Daemon._log_and_dump_message("Start Daemon...") locale.setlocale(locale.LC_ALL, 'C') os.environ["LC_ALL"]="C" # Get the pid from the pidfile try: pf = file(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if pid and linux.process_exists(pid): message = "Daemon already running, pid is %s\n" % pid Daemon._log_and_dump_message(message, sys.stderr) sys.exit(0) else: message = "pidfile %s does not exist. Daemon not running?\n" % self.pidfile Daemon._log_and_dump_message(message, sys.stderr) self.get_start_agent_by_name() Daemon._log_and_dump_message("configure hosts...") Daemon.configure_hosts() # Start the daemon self.daemonize() try: self.run() except Exception: content = traceback.format_exc() logger.error(content) sys.exit(1) print "Start Daemon Successfully"
def start(self): """ Start the daemon """ logger.debug("Start Daemon...") print "Start Daemon..." locale.setlocale(locale.LC_ALL, 'C') # Get the pid from the pidfile try: pf = file(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if pid and linux.process_exists(pid): message = "Daemon already running, pid is %s\n" sys.stderr.write(message % pid) sys.exit(0) else: message = "pidfile %s does not exist. Daemon not running?\n" sys.stderr.write(message % self.pidfile) self.get_start_agent_by_name() # Start the daemon self.daemonize() try: self.run() except Exception: content = traceback.format_exc() logger.error(content) sys.exit(1) print "Start Daemon Successfully"
def stop(self): """ Stop the daemon """ print "Stop Daemon..." #wait 2s for gracefully shutdown, then will force kill wait_stop = 2 # Get the pid from the pidfile try: pf = file(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: pid = None if not pid: message = "pidfile %s does not exist. Daemon not running?\n" sys.stderr.write(message % self.pidfile) return # not an error in a restart # Try killing the daemon process start_time = time.time() while 1: if linux.process_exists(pid): curr_time = time.time() if (curr_time - start_time) > wait_stop: os.kill(pid, SIGKILL) else: os.kill(pid, SIGTERM) time.sleep(0.3) else: if os.path.exists(self.pidfile): self.delpid() break print "Stop Daemon Successfully"