由于工作时使用到监控,因此看了一下监控进程实现逻辑
更新进度
- 2019.08.02 初次添加
记录ambari监测进程脚本:check_process_status.py
#!/usr/bin/env python
from resource_management.core.exceptions import ComponentIsNotRunning
from resource_management.core.logger import Logger
__all__ = ["check_process_status"]
import os
def check_process_status(pid_file):
"""
功能是检测进程是否正在运行
如果进程没有运行,将会抛出ComponentIsNotRunning异常
参数:pid_file (保存pid的文件)
"""
if not pid_file or not os.path.isfile(pid_file):
raise ComponentIsNotRunning()
with open(pid_file, "r") as f:
try:
pid = int(f.read())
except:
Logger.debug("Pid file {0} does not exist".format(pid_file))
raise ComponentIsNotRunning()
try:
os.kill(pid, 0)
except OSError:
Logger.debug("Process with pid {0} is not running. Stale pid file"
" at {1}".format(pid, pid_file))
raise ComponentIsNotRunning()
pass