Categories
Python Answers

How to create a daemon in Python?

Spread the love

Sometimes, we want to create a daemon in Python.

In this article, we’ll look at how to create a daemon in Python.

How to create a daemon in Python?

To create a daemon in Python, we can use the python-daemon library.

To install it, we run

pip install python-daemon

Then we can use it by writing

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("hello world")
            time.sleep(10)

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

to create a runner.DaemonRunner class with app to start the daemon with app.

And then we call do_axction to call the app‘s run method.

How to create a daemon in Python?

To create a daemon in Python, we can use the python-daemon library.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *