Beispiel #1
0
def start(config):
    if config.DAEMON:
        from pycopia import daemonize
        from pycopia import logfile
        lf = logfile.ManagedStdio(config.LOGFILENAME)
        daemonize.daemonize(lf, pidfile=config.PIDFILE)
    else:
        lf = sys.stdout
        fo = file(config.PIDFILE, "w")
        fo.write("%s\n" % (os.getpid(), ))
        fo.close()
    start_proc_manager(config, lf)
Beispiel #2
0
def start(config):
    setup(config)
    if config.DAEMON:
        from pycopia import daemonize
        from pycopia import logfile
        lf = logfile.ManagedStdio(config.LOGFILENAME)
        daemonize.daemonize(lf, pidfile=config.PIDFILE)
    else:
        lf = sys.stderr
        with open(config.PIDFILE, "w") as fo:
            fo.write("{}\n".format(os.getpid()))
    start_proc_manager(config, lf)
Beispiel #3
0
def run_server(argv):
    username = None
    do_daemon = True
    debug = False
    killserver = False
    try:
        optlist, args = getopt.getopt(argv[1:], "dnh?kl:f:p:s:")
    except getopt.GetoptError:
        print(run_server._doc % (argv[0], ))
        return

    if len(args) > 0:
        servername = args[0]
    else:
        servername = os.path.basename(argv[0])

    logfilename = "/var/log/%s.log" % (servername, )
    cffilename = "/etc/pycopia/%s.conf" % (servername, )
    pidfile = "/var/run/%s.pid" % (servername, )
    socketpath = '/tmp/%s.sock' % (servername, )

    for opt, optarg in optlist:
        if opt == "-n":
            do_daemon = False
        elif opt == "-k":
            killserver = True
        elif opt == "-d":
            debug = True
        elif opt == "-u":
            username = optarg
        elif opt == "-l":
            logfilename = optarg
        elif opt == "-f":
            cffilename = optarg
        elif opt == "-p":
            pidfile = optarg
        elif opt == "-s":
            socketpath = optarg
        elif opt in ("-h", "-?"):
            print(run_server._doc % (argv[0], ))
            return 2

    try:
        config = basicconfig.get_config(cffilename,
                                        CONFIGFILE=cffilename,
                                        PIDFILE=pidfile,
                                        SOCKETPATH=socketpath,
                                        LOGFILENAME=logfilename,
                                        DEBUG=debug,
                                        SERVERNAME=servername)
    except:
        ex, val, tb = sys.exc_info()
        logging.warn("Could not get server config: %s (%s)" % (ex, val))
        return 1

    if username:
        config.USERNAME = username

    if killserver:
        kill_server(config)
        return 0

    if check4server(config):
        logging.warn("Server %r already running on socket %r." %
                     (servername, socketpath))
        return 1

    if do_daemon and not debug:
        from pycopia import daemonize
        from pycopia import logfile
        lf = logfile.ManagedStdio(logfilename)
        daemonize.daemonize(lf, pidfile=pidfile)
    else:  # for controller
        fo = open(pidfile, "w")
        fo.write("%s\n" % (os.getpid(), ))
        fo.close()
        del fo

    server = get_server(config)
    return int(server.run())
Beispiel #4
0
    # log file is stdout and stderr, otherwise /dev/null
    if logfile is None:
        sys.stdout = open(DEVNULL, 'w')
        sys.stderr = open(DEVNULL, 'wb', 0)
    else:
        so = se = sys.stdout = sys.stderr = logfile
        os.dup2(so.fileno(), 1)
        os.dup2(se.fileno(), 2)
    return pid


if __name__ == "__main__":
    import time
    from pycopia import logfile
    lf = logfile.ManagedStdio("/var/tmp/test_daemonize", 1000, 5)
    #lf = None
    try:
        daemonize(lf)
    except SystemExit:
        pass
    except:
        import traceback
        ex, val, tb = sys.exc_info()
        print(ex, val)
        print("----")
        traceback.print_tb(tb)

    else:
        c = 0
        sys.stdout.write(b'Daemon stdout output\n')
Beispiel #5
0
def run_server(argv):
    username = None
    do_daemon = True
    debug = False
    killserver = False
    try:
        optlist, longopts, args = getopt.getopt(argv[1:], "dnh?kl:f:p:s:")
    except getopt.GetoptError:
        print(run_server._doc.format(procname=argv[0]))
        return

    if len(args) > 0:
        servername = args[0]
    else:
        servername = os.path.basename(argv[0])

    logfilename = "/var/log/{}.log".format(servername)
    cffilename = "/etc/pycopia/{}.conf".format(servername)
    pidfile = "/var/run/{}.pid".format(servername)
    socketpath = '/tmp/{}.sock'.format(servername)

    for opt, optarg in optlist:
        if opt == "-n":
            do_daemon = False
        elif opt == "-k":
            killserver = True
        elif opt == "-d":
            from pycopia import autodebug  # noqa
            debug = True
        elif opt == "-u":
            username = optarg
        elif opt == "-l":
            logfilename = optarg
        elif opt == "-f":
            cffilename = optarg
        elif opt == "-p":
            pidfile = optarg
        elif opt == "-s":
            socketpath = optarg
        elif opt in ("-h", "-?"):
            print(run_server._doc.format(procname=argv[0]))
            return 2

    try:
        config = basicconfig.get_config(cffilename,
                                        CONFIGFILE=cffilename,
                                        PIDFILE=pidfile,
                                        SOCKETPATH=socketpath,
                                        LOGFILENAME=logfilename,
                                        DEBUG=debug,
                                        SERVERNAME=servername)
    except:
        ex, val, tb = sys.exc_info()
        logging.warn("Could not get server config: {} ({})".format(
            ex.__name__, val))
        return 1
    config.update(longopts)

    if username:
        config.USERNAME = username

    if killserver:
        kill_server(config)
        return 0

    if check4server(config):
        logging.warn("Server {!r} already running on socket {!r}.".format(
            servername, socketpath))
        return 1

    if do_daemon and not debug:
        from pycopia import daemonize
        from pycopia import logfile
        lf = logfile.ManagedStdio(logfilename)
        daemonize.daemonize(lf, pidfile=pidfile)
    else:  # for controller
        with open(pidfile, "w") as fo:
            fo.write("{}\n".format(os.getpid()))

    server = get_server(config)
    return int(server.run())