def stop(self, pid, restart, msg = None): logging.info("Stopping Palabre...") if not pid: if self.daemon: sys.stderr.write("Could not stop, Palabre is not running (pid file '%s' is missing).\n" % self.pidfile) logging.warning("Could not stop, Palabre is not running (pid file '%s' is missing)." % self.pidfile) if not restart: logging.shutdown() logfile.close() sys.exit(1) else: ## There should be a better way, but i'm too lazy to search... but ## i'm sure there's a better way # if we're not a daemon we should clean up before committing suicide if not self.daemon: os.remove(self.pidfile) logging.shutdown() logfile.close() try: #global topServer #topServer.notifyStop() #time.sleep(1) print "sending signal..." os.kill(pid, signal.SIGUSR1) #os.system("kill -USR1 %d" % pid) print "signal sent" while True: print "in the signal loop", pid os.kill(pid,SIGTERM) os.kill(pid,signal.SIGKILL) time.sleep(1) except OSError, e: if e.strerror.find("No such process") >= 0: os.remove(self.pidfile) if not restart: logging.info("...stopped.") logging.shutdown() logfile.close() sys.exit(0) else: logging.error("%s" % e.strerror) logging.shutdown() logfile.close() sys.exit(1) except Exception, e: logging.error("%s" % e.strerror) logging.shutdown() logfile.close() sys.exit(1)
def start(self, msg = None): logging.info("Starting Palabre...") if not __debug__ and os.path.exists(self.pidfile): if self.daemon: sys.stderr.write("Could not start, Palabre is already running (pid file '%s' exists).\n" % self.pidfile) logging.warning("Could not start, Palabre is already running (pid file '%s' exists)." % self.pidfile) logging.shutdown() logfile.close() sys.exit(1) else: if self.daemon: # Do first fork. try: pid = os.fork () if pid > 0: # Exit first parent. sys.exit (0) except OSError, e: sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Decouple from parent environment. os.chdir("/") os.umask(0) os.setsid() # Do second fork. try: pid = os.fork() if pid > 0: # Exit second parent. sys.exit(0) except OSError, e: sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1)