python unix 服务库
我有一个命令行 python 应用程序,它是一项服务 - 即它等待连接并在收到请求时执行操作,例如网络服务器。有 python-daemon
库用于处理与终端分离、分叉等所有问题。但我想更进一步 - 让程序执行以下操作:
- 知道它是否是是否已经运行(通过检查 PID 文件)
- 当使用“start”选项调用时,如果守护进程未运行,则应启动该守护进程;如果正在运行,则告知现有守护进程的 PID 并退出。
- 当使用“stop”选项调用时,它应该杀死正在运行的实例(如果存在),并清理 PID 文件。
- 当使用“重新启动”选项调用时,它应该执行“停止”,然后“启动”。
- 当使用“状态”选项调用时,它应该显示正在运行的实例的 PID,如果没有运行,则不显示任何内容。
如果这看起来像标准的 Unix 服务——那正是我想要的。有没有实现这种模式的Python库?
I have a command-line python app that is a service - i.e. it waits for connections and does stuff when asked, like webserver. There's python-daemon
library for dealing with all the issues of detaching from the terminal, forking, etc. but I'd like to go a bit further - to have the program do the following:
- Know if it's already running or not (by checking the PID file)
- When called with 'start' option it should start the daemon if it's not running, or tell the PID of existing one and exit if running.
- When called with 'stop' option it should kill the running instance if it exist, and clean the PID file.
- When called with 'restart' option it should do 'stop', then 'start'
- When called with 'status' option it should display the PID of running instance or nothing if it's not running.
If that looks like standard Unix service - that's exactly what I want. Is there a Python library that implements such pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用过这个食谱。效果相当不错。
http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
I've used this recipe. Works pretty good.
http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/