【问题标题】:python-daemon start multiple instances of same program and passing in instance specific argumentspython-daemon 启动同一程序的多个实例并传入实例特定的参数
【发布时间】:2013-05-10 18:19:07
【问题描述】:

我有一个我写的日志挖掘工具。我可以使用 nohup 启动它并传入参数,例如要解析的文件(它使用我用 Python 编写的独立于平台的尾部类)。但是,我希望它作为初始化脚本或从命令行(作为守护进程)启动。如果在同一台服务器上查看多个日志文件,我希望能够启动多个实例。

我查看了python-daemon package,但在参考文档中不清楚是否可以传入进程/实例特定参数。例如就像程序的每个守护程序实例应该扫描的日志文件一样。

我试图解决的一个问题是如何停止或重新启动创建的各个守护程序实例。

【问题讨论】:

    标签: python python-daemon


    【解决方案1】:

    我今天不得不做同样的事情,多个(相同的)应用程序需要作为守护进程运行。 我没有使用新的 python-daemon 包,因为我从未使用过它,但我之前多次使用过 Sander Marechal (A simple unix/linux daemon in Python) 的 Daemon。

    我创建了一个简单的测试应用程序,不是最好的 python 代码,但它可以按预期工作。该示例使用了一个额外的参数,可以像这样使用:./runsample.py start <param>

    您将看到一个新的日志文件,以及在 /tmp 中为每个正在运行的守护进程创建的 pid 文件。

    您可以从这里获取 Daemon 类:A simple unix/linux daemon in Python

    测试应用

    import sys, time
    from daemon import Daemon
    
    #simple test app that writes to a file every second
    #this is just to check that the correct daemons are running
    
    class testapp(Daemon):
    
        ID = 0
    
        def __init__(self, id):
            print 'Init (ID): ' + id
            #set the params
            self.ID = id
            #set the pid file
            pid = '/tmp/testapp-' + str(id) + '.pid'
            #init the base with the pid file location
            Daemon.__init__(self, pid)
    
    
        #this is the overwritten method from the article by Sander Marechal
        # http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
        def run(self):
            while True:
                #open file, append mode
                f = open('/tmp/log-' + self.ID + '.log', 'a')
                #write
                f.write(str(time.time()))
                #close
                f.close()
                #wait
                time.sleep(1)
    

    初始化脚本/守护进程

    #!/usr/bin/env python
    
    #
    # Multiple daemons for the same app test
    #
    
    import sys
    from testapp import testapp
    
    #check is anough arguments are passed
    if len(sys.argv) != 3:
        print "usage: %s start|stop|restart <param>" % sys.argv[0]
        sys.exit(2)
    
    #get the extra arguments
    id = sys.argv[2]
    print 'Param (ID): ' + sys.argv[2]
    #start the app with the parameters
    daemon = testapp(id)
    
    
    #from the article by Sander Marechal
    # http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
    if len(sys.argv) == 3:
        if 'start' == sys.argv[1]:
            print 'Start'
            daemon.start()
        elif 'stop' == sys.argv[1]:
            daemon.stop()
            print 'Stop'
        elif 'restart' == sys.argv[1]:
            print 'Restarting...'
            daemon.restart()
            print 'Restarted'
        else:
            print "Unknown command"
            sys.exit(2)
        sys.exit(0)
    else:
        print "usage: %s start|stop|restart" % sys.argv[0]
    sys.exit(2)
    

    我希望这也适用于你。

    【讨论】:

    • 谢谢...我现在正在处理其他事情(让它运行,但只有一个文件...这并不理想,但现在可以工作),但想回到这个。有机会我会试试的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多