How do you create a daemon in Python?

后端 未结 16 1876
轻奢々
轻奢々 2020-11-22 01:55

Searching on Google reveals x2 code snippets. The first result is to this code recipe which has a lot of documentation and explanation, along with some useful discussion und

16条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 02:48

    Here's my basic 'Howdy World' Python daemon that I start with, when I'm developing a new daemon application.

    #!/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:
                print("Howdy!  Gig'em!  Whoop!")
                time.sleep(10)
    
    app = App()
    daemon_runner = runner.DaemonRunner(app)
    daemon_runner.do_action()
    

    Note that you'll need the python-daemon library. You can install it by:

    pip install python-daemon
    

    Then just start it with ./howdy.py start, and stop it with ./howdy.py stop.

提交回复
热议问题