Python daemonize

纵然是瞬间 提交于 2019-12-07 01:55:18

问题


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:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/3383741/python-daemonize

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!