我有一个使用旧版python-daemon和这段代码创建的基本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:
print("Howdy! Gig'em! Whoop!")
time.sleep(10)
app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
现在一切正常但我需要向守护进程添加一个可能的命令。当前的默认命令是“start,stop,restart”。我需要第四个命令“mycommand”,它只会在执行时运行此代码:
my_function()
print "Function was successfully run!"
我尝试过谷歌搜索和研究,但无法自己解决。我尝试手动获取参数而不会陷入使用sys.argv
的python-daemon代码,但无法使其正常工作。
答案 0 :(得分:0)
查看转轮模块的代码,以下内容应该有效......我在定义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()