3

Okay I have looked at python-daemon, and also at various other daemon related code recipes. Are there any 'hello world' tutorials out there that can help me get started using a python based daemonized process?

5
  • The example on python-daemon's page (pypi.python.org/pypi/python-daemon) doesn't work for you ? Commented Oct 6, 2011 at 14:22
  • 3
    Please strongly consider not daemonizing yourself. There's far better ways of making processes run in the background, such as djb's daemontools, launchd, or upstart. They even handle important things like logging for you, and make sure your process stays running. Commented Oct 6, 2011 at 14:59
  • @AaronGallagher why should I avoid creating a daemon on my own I have heard that several times before? Commented Oct 6, 2011 at 18:33
  • 1
    Because nothing is managing the process. If you use a system utility like launchd, the system can be stopped and restarted automatically Commented Oct 6, 2011 at 19:42
  • See this link: creating-a-daemon-the-python-way -by Chad J. Schroeder Commented Oct 11, 2012 at 16:34

2 Answers 2

5

The PEP 3143 contains several examples, the simplest one of which is:

import daemon

from spam import do_main_program

with daemon.DaemonContext():
    do_main_program()

This seems as straightforward as it gets. If there's something that's unclear, please pose specific questions.

Sign up to request clarification or add additional context in comments.

1 Comment

after doing just that do you just simple run it as a simple python script? python2.6 foo.py?
-3

Using subprocess.Popen, you can launch another process that will survive your current process...

In a python console run :

import subprocess
subprocess.Popen(["/bin/sh", "-c", "sleep 500"])

Kill your console, look at existing processes, sleep is alive...

4 Comments

I have never seen this approach before I will look into it thanks.
This will not work. It only seems to work with sleep because the sleep itself is holding the process open. Try it with calling an actually application. Once you close the terminal the process will die.
If you really want to accomplish this by calling out to the shell you can do: subprocess.Popen(["nohup", "<program here>"]) which will launch the command freely and it wont die if the terminal is closed.
This is not actually daemonizing anything. Only apps that will stay open with a subprocess call are apps that are already a daemon. This is merely a call to open another program, not a method to daemonize a Python application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.