Python守护进程 [英] Python daemonize

查看:249
本文介绍了Python守护进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想守护一个python进程,现在想问一问,运行一个守护进程(如父进程)并调用另一个打开10-30个线程的类是否是一种好习惯。

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.

我打算为服务器组编写监视脚本,并且希望每5分钟检查一次每台服务器,每台服务器都准确检查5分钟。

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.

我想用这种方式(某种程度上说,ps auxf风格的输出):

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

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

....

谢谢!

推荐答案

您可以为此使用 supervisord 。您可以配置任务以响应事件。这些事件可以通过监视过程来手动创建或自动创建,也可以根据固定的时间间隔自动创建。

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.

它是完全可定制的,并且用Python编写。

It is fully customizable and written in Python.

示例:

[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...

或者如果您想要事件监听器要响应流程输出,请每分钟使用事件 PROCESS_COMMUNICATION_STDOUT TICK_60 进行检查。日志可以重定向到文件等,因此您可以始终查看状态。

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.

这篇关于Python守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆