操作系统: CentOS 6.5_x64
开发语言: Python
适用场景:
程序异常退出后需要及时启动的情况。
源码地址: https://github.com/mike-zhang/processGuarder
原理
通过ps检查进程是否存在,如果不存在则启动
使用
./processGuarder.py default.xml
参数配置
全局参数配置
-
interval
检测时间间隔
-
loglevel
日志级别
0 : NOTSET,
1 : DEBUG,
2 : INFO,
3 : WARNING,
4 : ERROR,
5 : CRITICAL
-
lognum
日志文件数目
-
logsize
日志文件大小(单位:MB)
-
logPath
日志文件路径
-
psTimes
ps命令执行最大次数(连续检查psTimes次,如果还是没检测到进程,则启动)
程序参数配置
- processName
进程名称
- command
程序路径
- param
程序运行参数
- restartDelay
程序启动需要的时间(单位:秒)
配置示例
普通程序配置(example):
<program scriptName = "">
<processName>proName</processName>
<command>/path/to/pro</command>
<param>params</param>
<restartDelay>1</restartDelay>seconds
</program>
脚本程序配置(example):
<program scriptName="python" >
<processName>script.py</processName>
<command>/path/to/script.py</command>
<param>params</param>
<restartDelay>1</restartDelay>seconds
</program>
其它
1、进程守护器前台运行还是后台运行?
答 : 进程守护器启动时会通过fork方式生成守护进程,在后台运行。
2、如果进程守护器程序本身退出了怎么办?
答 : 可以在crontab中配置定时启动进程守护器,如果守护器已经启动,会自动退出,不会启动多份;如果进程守护器没有启动,则正常启动,并根据提供的配置进行程序检测。
本文github地址:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170118_小工具之进程守护器.md
欢迎补充