def checkServer(force=False, restart=True): try: if force: raise ServerDownError("(re)start or shutdown forced!") print time.asctime(time.localtime()),"checking Snakelets server. Trying to PING to %s:%d ..." %(SERVER_HOST, serv.HTTPD_PORT), tryConnect(SERVER_HOST, serv.HTTPD_PORT) print "Server is alive." except ServerDownError,x: print "Server down: ",x print "Killing server" try: daemon.startstop(stdout=SERVER_LOG, stderr=SERVER_ERR_LOG, pidfile=SERVER_PID, action='stop') except daemon.DaemonizeError,x: print "Ignoring error: ",x
def main(args): if len(args)>1: if args[1]=='check': checkServer() elif args[1] =='stop': force=False print "stopping server..." try: daemon.startstop(stdout=SERVER_LOG,pidfile=SERVER_PID,action='stop') except daemon.DaemonizeError,x: print "POSSIBLE ERROR:",x force=True checkServer(force=force,restart=False) print "\nServer is not running anymore." elif args[1] =='force': print ">>forcing the start of another server<<" checkServer(force=True) else: print "Usage: %s [check|stop|force]" % args[0] print "(check will restart the server if it's down)"
for o, a in opts: if o == '-h': print __doc__ sys.exit() if o == '-c': configFile = a if o == '-f': doNotFork = True myConfigManager = config_manager.ConfigManager(configFile) # we instance it here so its before we go into the background myUniFiLab = UniFiLab(myConfigManager) # on non Windows Systems we go into the background if not doNotFork and sys.platform in ("linux2", "darwin"): daemon.startstop(myConfigManager.getErrorLogFile(), pidfile=myConfigManager.getPidFile()) # configure the logging _handler = logging.handlers.RotatingFileHandler( myConfigManager.getLogFile(), maxBytes=10 * 1024**2, backupCount=5) _handler.setFormatter( logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) log.addHandler(_handler) log.info('Started') myUniFiLab.continuousLoop() log.info('Stopped') if __name__ == '__main__': main()
try: daemon.startstop(stdout=SERVER_LOG, stderr=SERVER_ERR_LOG, pidfile=SERVER_PID, action='stop') except daemon.DaemonizeError,x: print "Ignoring error: ",x checkIfPortIsFree(serv.HTTPD_PORT) if restart: print "Restarting... workdir=",SERVER_DIR os.chdir(SERVER_DIR) daemon.startstop(stdout=SERVER_LOG, stderr=SERVER_ERR_LOG, pidfile=SERVER_PID, action='start') print "%s GMT: This is the daemon." % time.asctime(time.gmtime()) try: serv.main(workingdir=SERVER_DIR) except: print >>sys.stderr,"\n**** ERROR OCCURRED IN SERVER! (%s GMT) ****" % time.asctime() traceback.print_exc(sys.stderr) print >>sys.stderr,"**** END OF ERROR, EXITING! ****\n" print "%s GMT: an error occured, see the error log!" % time.asctime(time.gmtime()) print "%s GMT: Daemon ends." % time.asctime(time.gmtime()) def killPids(pids): from signal import SIGINT,SIGTERM,SIGKILL for pid in pids: try: pid=int(pid)
for o, a in opts: if o == "-h": print __doc__ sys.exit() if o == "-c": configFile = a if o == "-f": doNotFork = True myConfigManager = config_manager.ConfigManager(configFile) # we instance it here so its before we go into the background myUniFiLab = UniFiLab(myConfigManager) # on non Windows Systems we go into the background if not doNotFork and sys.platform in ("linux2", "darwin"): daemon.startstop(myConfigManager.getErrorLogFile(), pidfile=myConfigManager.getPidFile()) # configure the logging _handler = logging.handlers.RotatingFileHandler( myConfigManager.getLogFile(), maxBytes=10 * 1024 ** 2, backupCount=5 ) _handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) log.addHandler(_handler) log.info("Started") myUniFiLab.continuousLoop() log.info("Stopped") if __name__ == "__main__": main()
daemon.startstop(stdout=SERVER_LOG, stderr=SERVER_ERR_LOG, pidfile=SERVER_PID, action='stop') except daemon.DaemonizeError, x: print "Ignoring error: ", x checkIfPortIsFree(serv.HTTPD_PORT) if restart: print "Restarting... workdir=", SERVER_DIR os.chdir(SERVER_DIR) daemon.startstop(stdout=SERVER_LOG, stderr=SERVER_ERR_LOG, pidfile=SERVER_PID, action='start') print "%s GMT: This is the daemon." % time.asctime(time.gmtime()) try: serv.main(workingdir=SERVER_DIR) except: print >> sys.stderr, "\n**** ERROR OCCURRED IN SERVER! (%s GMT) ****" % time.asctime( ) traceback.print_exc(sys.stderr) print >> sys.stderr, "**** END OF ERROR, EXITING! ****\n" print "%s GMT: an error occured, see the error log!" % time.asctime( time.gmtime()) print "%s GMT: Daemon ends." % time.asctime(time.gmtime()) def killPids(pids):