Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将参数传递给守护进程运行程序_Python_Daemon - Fatal编程技术网

在python中将参数传递给守护进程运行程序

在python中将参数传递给守护进程运行程序,python,daemon,Python,Daemon,我有一个这样的守护程序脚本 #!/usr/bin/python import time from daemon import runner class App(): def __init__(self): self.stdin_path = '/dev/null' self.stdout_path = '/dev/tty' self.stderr_path = '/dev/tty' self

我有一个这样的守护程序脚本

#!/usr/bin/python
import time
from daemon import runner

class App():
    def __init__(self):
            self.stdin_path = '/dev/null'
            self.stdout_path = '/dev/tty'
            self.stderr_path = '/dev/tty'
            self.pidfile_path =  '/tmp/foo.pid'
            self.pidfile_timeout = 5

    def run(self):
            while True:
                    self.manage_process()
                    time.sleep(5)

    def manage_process(self):
            initial_list = self.get_process_list()
            for x in initial_list:
                    print x.get_process_name()

    def get_process_list(self):
            return process_list() //Gives the list of processes

def main():
    opts = getopts() //module to parse cmdline arguments 
    //opts.action will have start if command ran is "sample_prog.py --action start"
    app = App()
    daemon_runner = runner.DaemonRunner(app)
    daemon_runner.do_action()

if __name__ == '__main__':
    main()
问题是,我有一个单独的模块来解析命令行参数。使用它,我可以给出如下参数,
sample\u prog.py--action start

我无法将收到的值(start | stop | restart)传递给守护进程运行程序。有人能告诉我有什么办法吗?

请执行以下操作

import sys
if __name__ == '__main__':
    main(sys.argv)

def main(arguments):
    # use arguments here
    opts = getopts() //module to parse cmdline arguments 
    //opts.action will have start if command ran is "sample_prog.py --action start"
    app = App()
    daemon_runner = runner.DaemonRunner(app)
    daemon_runner.do_action()
同样地定义main,它将为您提供一个要传递的参数列表

import sys
if __name__ == '__main__':
    main(sys.argv)

def main(arguments):
    # use arguments here
    opts = getopts() //module to parse cmdline arguments 
    //opts.action will have start if command ran is "sample_prog.py --action start"
    app = App()
    daemon_runner = runner.DaemonRunner(app)
    daemon_runner.do_action()

同样地定义main,它将为您提供一个要传递的参数列表

保持守护进程运行、防止它消耗所有资源、防止它让僵尸进程困扰您的系统等等都需要大量的簿记工作

下面是我们正在运行的最简单守护进程的简化版本(它使许多工作进程保持运行和循环)。它调用commandq.run_command()来执行实际工作(不包括)

如果您可以使用cron作业,那么这将容易得多(您将需要cron作业或类似的作业来验证守护进程是否正在运行)


为了使一个后台进程保持运行,防止它消耗所有资源,防止它让僵尸进程困扰您的系统,需要做大量的簿记工作

下面是我们正在运行的最简单守护进程的简化版本(它使许多工作进程保持运行和循环)。它调用commandq.run_command()来执行实际工作(不包括)

如果您可以使用cron作业,那么这将容易得多(您将需要cron作业或类似的作业来验证守护进程是否正在运行)


你不是已经在用
getopts()
函数做这些了吗?我可以得到参数了。。但是我不知道如何将其传递给runnerAren您不是已经在使用
getopts()
函数执行此操作了吗?我能够获取参数。。但我不知道如何将其传递给Runner请在main()的函数定义中定义变量,那么我如何将其传递给DaemonRunner?python filename.py start。请在main()的函数定义中定义变量,那么我如何将其传递给DaemonRunner?python filename.py start。