我有一个基本的Python守护进程,使用旧版本的python-daemon和以下代码创建:
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:
print("Howdy! Gig'em! Whoop!")
time.sleep(10)
app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()现在一切正常,但我需要向我的守护进程再添加一个可能的命令。当前的默认命令是“启动、停止、重新启动”。我需要第四个命令"mycommand“,它在执行时只运行以下代码:
my_function()
print "Function was successfully run!"我试过用谷歌搜索和研究,但我自己都搞不清楚。我试着用sys.argv手动提取参数,而不是陷入python守护程序代码中,但我不能让它工作。
发布于 2016-09-20 04:58:15
看一下runner模块的代码,下面的代码应该是有效的…我在定义stdout和stderr时遇到了问题...你能测试一下吗?
from daemon import runner
import time
# Inerith from the DaemonRunner class to create a personal runner
class MyRunner(runner.DaemonRunner):
def __init__(self, *args, **kwd):
super().__init__(*args, **kwd)
# define the function that execute your stuff...
def _mycommand(self):
print('execute my command')
# tell to the class how to reach that function
action_funcs = runner.DaemonRunner.action_funcs
action_funcs['mycommand'] = _mycommand
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:
print("Howdy! Gig'em! Whoop!")
time.sleep(10)
app = App()
# bind to your "MyRunner" instead of the generic one
daemon_runner = MyRunner(app)
daemon_runner.do_action()https://stackoverflow.com/questions/39581046
复制相似问题