2016-09-19 69 views
2

我有使用Python守護程序的舊版本創建了一個基本的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!" 

我曾嘗試谷歌搜索和研究,但無法弄清楚我自己。我嘗試手動拾取參數,而不會陷入python-daemon代碼中,但是不能使其工作。

+0

你有沒有得到這個工作?你嘗試過Riccardo的解決方案嗎? –

回答

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()