tags:

views:

94

answers:

3
+2  Q: 

Python daemonize

Hi,

I would like to daemonize a python process, and now want to ask if it is good practice to have a daemon running, like a parent process and call another class which opens 10-30 threads.

I'm planning on writing a monitoring script for group of servers and would like to check every server every 5 mins, that each server is checked exactly 5minutes.

I would like to have it this way ( sort of speak, ps auxf style output ):

|monitor-daemon.py
 \-check-server.py
 \-check-server.py

....

Thank you!

+1  A: 

Maybe you should use http://pypi.python.org/pypi/python-daemon

Tony Veijalainen
A: 

You can use supervisord for this. You can configure tasks to respond to events. The events can be manually created or automatically by monitoring processes or based on regular intervals.

It is fully customizable and written in Python.

Example:

[program:your_daemon_name]
command=your_daemon_process
# Add extra options here according to the manual...

[eventlistener:your_monitor_name]
command=your_monitor_process
events=PROCESS_STATE_RUNNING # Will be triggered after a program changes from starting to running
# Add extra options here according to the manual...

Or if you want the eventlistener to respond to the process output use the event PROCESS_COMMUNICATION_STDOUT or TICK_60 for a check every minute. The logs can be redirected to files and such so you can always view the state.

WoLpH
Thanks, but this looks like an overkill for me (at this moment) - if you have any suggestion for the following:(sorry if repeating myself) - Daemon is started, checks if everything is fine - Calls (example) MyMonitor().check() -- MyMonitor.check() has tasks - gets data from a DB ( SQLite/MySQL ) - passes data and open Threads for each data entry - based on the output further actions are there (failed port - send email ) - closes created ThreadsSorry for so n00bish style but want to check if everything is in place, before using additional modules.
abiko
I've added an example abiko, perhaps that will help. See the docs for an example event listener: http://supervisord.org/events.html#example-event-listener-implementation
WoLpH
A: 

There's really not much to creating your own daemonize function: The source for Advanced Programming in the Unix Environment (2nd edition) is freely available: http://www.apuebook.com/src.tar.gz -- you're looking for the apue.2e/daemons/init.c file.

There is a small helper program that does all the work of creating a proper daemon, it can be used to wrap arbitrary programs; this might save some hassle.

sarnold