2016-09-19 11 views
2

나는 파이썬 데몬의 이전 버전을 사용하여 만든 기본 파이썬 데몬이 코드가 있습니다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으로 파이썬 데몬 코드에 침입하지 않고 수동으로 인수를 시도했지만 작동하지 못했습니다.

+0

혹시이 작업을 받으셨어요? Riccardo의 솔루션을 사용해 보셨습니까? –

답변

0

나는 표준 출력을 정의하고 표준 오류 ... 당신이 그것을 테스트 할 수있는 문제는 ... 작업해야 다음, 주자 모듈의 코드를 한하십니까?

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