Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Rather than writing your own daemon, use <a href="http://pypi.python.org/pypi/python-daemon" rel="noreferrer">python-daemon</a> instead! <a href="http://pypi.python.org/pypi/python-daemon" rel="noreferrer">python-daemon</a> implements the well-behaved daemon specification of <a href="http://python.org/dev/peps/pep-3143" rel="noreferrer">PEP 3143</a>, "Standard daemon process library". </p> <p>I have included example code based on the accepted answer to this question, and even though the code looks almost identical, it has an important fundamental difference. Without <a href="http://pypi.python.org/pypi/python-daemon" rel="noreferrer">python-daemon</a> you would have to use <code>&amp;</code> to put your process in the background and <code>nohup</code> and to keep your process from getting killed when you exit your shell. Instead this will automatically detach from your terminal when you run the program.</p> <p>For example:</p> <pre><code>import daemon import time def do_something(): while True: with open("/tmp/current_time.txt", "w") as f: f.write("The time is now " + time.ctime()) time.sleep(5) def run(): with daemon.DaemonContext(): do_something() if __name__ == "__main__": run() </code></pre> <p>To actually run it:</p> <pre><code>python background_test.py </code></pre> <p>And note the absence of <code>&amp;</code> here.</p> <p>Also, <a href="https://stackoverflow.com/questions/473620/how-do-you-create-a-daemon-in-python">this other stackoverflow answer</a> explains in detail the many benefits of using <a href="http://pypi.python.org/pypi/python-daemon" rel="noreferrer">python-daemon</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload