6

Does anyone know of a working and well documented implementation of a daemon using python? Please post a link here if you know of a project that fits these two requirements.

1
  • env is ubuntu 10.10, python 2.6 Commented Oct 22, 2010 at 20:00

3 Answers 3

4

Three options I can think of-

  1. Make a cron job that calls your script. Cron is a common name for a GNU/Linux daemon that periodically launches scripts according to a schedule you set. You add your script into a crontab or place a symlink to it into a special directory and the daemon handles the job of launching it in the background. You can read more at wikipedia. There is a variety of different cron daemons, but your GNU/Linux system should have it already installed.
  2. Pythonic approach (a library, for example) for your script to be able to daemonize itself. Yes, it will require a simple event loop (where your events are timer triggering, possibly, provided by sleep function). Here is the one I recommend & use - A simple unix/linux daemon in Python
  3. Use python multiprocessing module. The nitty-gritty of trying to fork a process etc. are hidden in this implementation. It's pretty neat.

I wouldn't recommend 2 or 3 'coz you're in fact repeating cron functionality. The Linux system paradigm is to let multiple simple tools interact and solve your problems. Unless there are additional reasons why you should make a daemon (in addition to trigger periodically), choose the other approach.

Also, if you use daemonize with a loop and a crash happens, make sure that you have logs which will help you debug. Also devise a way so that the script starts again. While if the script is added as a cron job, it will trigger again in the time gap you kept.

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

7 Comments

I've been trying #2 without success. it's like once the daemon forks itself, I can't reference my django project anymore. thinking it's a path issue. see stackoverflow.com/questions/3992175/…
did you follow the code example in the link I gave? That works fine for me.
yes, I followed it. downloaded his daemon.py, subclassed it. when trying to import my django project it fails.
"Two options" numbered "1.", "2." and "3.". What's wrong with this picture?
@S.Lott Started with 2 got one more option :) I guess it's like buy 2 get 1 free ;)
|
4

If you just want to run a daemon, consider Supervisor, a daemon that itself controls and manages daemons.

If you want to look at the nitty-gritty, you can check out Supervisor's launch script or some of the responses to this lazyweb request.

Comments

1

Check this link for a double-fork daemon: http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/

The code is readable and well-documented. You want to take a look at chapter 13 of W. Richard's book 'Advanced Programming in the UNix Environment' for detailed information on Unix daemons.

Comments

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.