提交 f626b284 authored 作者: 陈泽健's avatar 陈泽健

refactor(系统服务进程监测): 优化 JAR 进程检测方法

- 使用 pgrep -f 命令替代复杂的 ps 和 grep 组合,提高检测准确性和效率
- 获取 JAR进程的完整信息,包括 PID、状态和完整命令
-统一 JAR 进程和普通进程的检测逻辑,提高代码可维护性
上级 0f2d1ee8
...@@ -419,30 +419,36 @@ class ServerProcessMonitor: ...@@ -419,30 +419,36 @@ class ServerProcessMonitor:
# 2. 智能进程检测 # 2. 智能进程检测
if process_name.endswith('.jar'): if process_name.endswith('.jar'):
# 2.1 JAR进程精确检测 # 2.1 JAR进程精确检测 - 使用pgrep -f进行全命令匹配
cmd = ( stdin, stdout, stderr = ssh.exec_command(
f"ps -ef | " f"pgrep -f '{process_name}' || echo ''"
f"grep '{process_name}' | " # 匹配完整JAR路径
f"grep -v grep | " # 排除grep进程自身
f"head -1" # 只取第一个匹配结果
) )
stdin, stdout, stderr = ssh.exec_command(cmd) pid = stdout.read().decode().strip()
output = stdout.read().decode().strip()
if pid:
if output: # 获取完整进程信息
parts = output.split() stdin, stdout, stderr = ssh.exec_command(
return { f"ps -p {pid} -o pid,stat,cmd"
'is_container': False, )
'running': True, process_info = stdout.read().decode().strip()
'pid': parts[1], # PID
'status': parts[7], # 状态 if process_info:
'command': ' '.join(parts[7:]), # 完整命令 lines = process_info.split('\n')
'process_type': 'java' if len(lines) > 1:
} parts = lines[1].split()
return {
'is_container': False,
'running': True,
'pid': parts[0], # PID
'status': parts[1], # 状态
'command': ' '.join(parts[2:]), # 完整命令
'process_type': 'java'
}
else: else:
# 2.2 普通进程检测 # 2.2 普通进程检测 - 使用pgrep -f进行全命令匹配
stdin, stdout, stderr = ssh.exec_command( stdin, stdout, stderr = ssh.exec_command(
f"pgrep -f '{process_name}' || echo ''" # 使用pgrep -f进行全命令匹配 f"pgrep -f '{process_name}' || echo ''"
) )
pid = stdout.read().decode().strip() pid = stdout.read().decode().strip()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论