Monit 6.0.0 常用 CLI 命令与工具详解

Monit 6.0.0 常用 CLI 命令与工具详解:monit 命令行与守护进程通信模型、status/summary/report/validate 状态查询、start/stop/restart/monitor/unmonitor 动作控制、-g 分组批量操作、reload/-t/-H/--id/procmatch 配置管理、quit 停机语义与 Web XML 接口,全部命令附 Ubuntu 22.04 实测回显。

MonitMonit监控2026/10/20

Monit 6.0.0 常用 CLI 命令与工具详解

‍‍​​‌​> 目标:把 Monit 6.0.0 的 monit 命令行工具用全、用对——状态查询、动作控制、配置管理、批量运维与排障。本篇全部命令与回显在 Ubuntu 22.04.5 LTS(主机名 app-server,官方二进制版 Monit 6.0.0)上实测通过;监控规则的配置与部署见《Monit 6.0.0 部署指南》。

一、环境信息

项目值
操作系统Ubuntu 22.04.5 LTS(x86_64),主机名 app-server
Monit 版本6.0.0(官方预编译二进制)
命令路径/usr/local/bin/monit → /usr/local/monit/monit
控制文件/etc/monitrc(软链 → /etc/monit/monitrc,0700)
守护进程systemd 托管 monit.service,控制台 http://10.89.58.3:2812(admin/q1w2e3r4)
已部署监控对象app-server(System)、sshd / fake_app(Process)、passwd_file(File)、rootfs(Filesystem)、demo_probe(Program),分组 core-services / probes

二、CLI 与守护进程的通信模型

Monit 命令行的运行分两种形态,理解它才能避免最常见的 Connection refused:

  1. 经 HTTP 通道与守护进程交互:monit status、monit summary、monit start/stop/restart/monitor/unmonitor/reload/validate/quit 等命令,本质是 CLI 按 monitrc 中 set httpd ... use address 声明的地址向 2812 端口发请求——所以这些命令要求守护进程在跑且控制台已配置。实测连接目标即绑定地址(本环境 10.89.58.3);
  2. 本地独立执行:monit -t(语法检查)、monit -H <文件>(哈希计算)、monit procmatch <正则>(匹配预演)不依赖守护进程,任何时刻可用。

排障实录:守护进程停掉后执行查询命令的真实回显:

systemctl stop monit
monit summary

实测输出:

Cannot connect to [10.89.58.3]:2812 -- Connection refused

💡 技术拓展:为什么 Monit CLI 走 HTTP 而不是本地信号/套接字?——守护进程是唯一持有完整监控状态、规则表与动作队列的实体,走统一 HTTP 通道让 CLI、浏览器、远程脚本、M/Monit 四类客户端共享同一套鉴权(allow 规则)与同一份状态视图,也天然支持从别的主机发控制命令(放行来源网段即可)。这是"控制平面即服务"的早期简洁实现。

三、命令总览与语法

monit -h 实测输出:

Usage: monit [options]+ <command> [options]
Options are as follows:
  -c file       Use specified control file
  -d N          Run as a daemon once per N second
  -g name       Set group name for start, stop, restart, monitor and unmonitor
  -I            Run in the foreground (Run from init)
  -l file       Print log information to file
  -p pidfile    Use alternate pid file in daemon or reset mode
  -s statefile  Set statefile to explicit file
  -t            Run syntax check for the control file
  -v            Verbose mode, work noisy (also to logfile)
  -v2           Very verbose mode, same as -v, log stacktrace on error
  -vv           Very very verbose mode, same as -v2, add engine details
  -H [file]     Print SHA256 and MD5 hashes of the file or of stdin if no
                file specified
  -B            Do not display color and table output
  --id          Print Monit's ID
  --resetid     Reset Monit's ID. Use with caution
  --version     Print version information

Possible arguments are as follows:
  start all            Start all services
  start <name>         Start a service, a service group, or services by
                       type: 'start system|process|file|fifo|filesystem|
                       directory|host|network|program|status'
  stop all             Stop all services
  stop <name>          Stop a service or a service group (never stops
                       the whole system group)
  restart all          Restart all services
  restart <name>       Restart a service, a service group, or services
                       by type: 'restart system|process|file|fifo|
                       filesystem|directory|host|network|program|status'
  monitor all          Enable monitoring of all services
  monitor <name>       Enable monitoring of a service, a service group,
                       or services by type
  unmonitor all        Disable monitoring of all services
  unmonitor <name>     Disable monitoring of a service, a service group,
                       or services by type
  status [name]        Print status of a service or the whole system
  status summary       Print short status of all services
  summary [name]       Print short status of a service or the whole
                       system
  report [up|down|...] Report state of services. Supported values are
                       'up', 'down', 'initializing', 'unmonitored',
                       'failed', 'timeout', 'offline' and 'doesnotexist'
  reload               Reinitialize a daemon process
  validate             Check all services and start with not running
                       services in active mode
  procmatch <regex>    Test a process match pattern
  quit                 Kill the Monit daemon process

💡 注意:-h 里 start <name> 描述的"by type"用法(如 monit start process)实测在 6.0.0 二进制中并不如预期——执行 monit summary process 得到的是 Service 'process' not found(6.0.0 的 summary/status/monitor 等命令的 [name] 参数按服务名解析,而非类型关键字),按类型操作请用 -g <分组>。这是 6.0.0 实测行为,遇旧版文章的示例失效时先按本条核对。

常用组合速查:

场景命令
看全部对象短状态monit summary
看某对象完整状态monit status sshd
改配置后生效monit -t && monit reload
重启某进程monit restart sshd
分组批量操作monit -g probes restart
维护期暂停/恢复监控monit unmonitor all / monit monitor all
哈希计算monit -H /etc/passwd
停守护进程systemctl stop monit(见 quit 语义说明)

四、状态查询类命令详解

4.1 monit summary —— 全对象短状态

monit summary

实测输出:

Monit 6.0.0 uptime: 12m
 Service Name                     Status                      Type
 app-server                       OK                          System
 sshd                             OK                          Process
 fake_app                         OK                          Process
 passwd_file                      OK                          File
 rootfs                           OK                          Filesystem
 demo_probe                       OK                          Program

首行显示守护进程版本与运行时长;状态列取值:OK(正常)、Status failed(测试未过)、Not monitored(已暂停)、Initializing(启动延迟期)、Timeout(熔断)、Does not exist(进程缺失)、Connection failed(端口失败)等。异常排查入口:先看这里哪个对象不是 OK,再用 monit status <名> 看细节、用日志看动作记录。

传服务名可过滤单对象(实测):

monit summary sshd
Monit 6.0.0 uptime: 16m
 Service Name                     Status                      Type
 sshd                             OK                          Process

4.2 monit status —— 完整状态明细

monit status 不带参数输出全部对象的完整明细;带名字输出单个对象。实测(System 类,含 6.0.0 新增的 swap 换页字段):

monit status app-server
Monit 6.0.0 uptime: 12m

System 'app-server'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [1.79] [1.56] [1.54]
  cpu                          8.7%usr 5.2%sys 0.0%nice 0.3%iowait 0.0%hardirq 1.1%softirq 0.0%steal 0.0%guest 0.0%guestnice
  memory usage                 8.0 GB [25.6%]
  swap usage                   524 kB [0.0%]
  swap pagein/s                0
  swap pageout/s               0
  uptime                       7h 49m
  boot time                    Mon, 19 Oct 2026 15:47:22
  filedescriptors              11424
  data collected               Mon, 19 Oct 2026 23:37:02

Process 类状态含 pid/parent pid/uptime/threads/cpu/memory/filedescriptors/IO 速率与端口响应时间等字段,File 类含权限/校验和/时间戳,Program 类含 last exit value/last output(各类对象的完整字段解读见《Monit 6.0.0 部署指南》第五节)。

4.3 monit report —— 按状态汇总计数

monit report

实测输出:

Monit 6.0.0 uptime: 16m
AppServer                 Process          Filesystem    File              Program
--------------------------------------------------------------------------------------
[0x0] app-server          [0x0] sshd       [0x0] rootfs  [0x0] passwd_file [0x0] demo_probe
                          [0x0] fake_app
--------------------------------------------------------------------------------------
 Up: 1                    Up: 2            Up: 1         Up: 1             Up: 1
 Down: 0                  Down: 0          Down: 0       Down: 0           Down: 0

按类型分列统计 up/down,monit report down 则只列出异常对象,适合脚本巡检快速判断(exit code + 输出双重消费)。

4.4 monit validate —— 立即全量校验

不等下个轮询周期,立即执行一轮全量校验(并唤醒守护进程):

monit validate

实测输出(节选,前两行 + 末行):

Monit daemon with PID 2825 awakened
-------------------------------------------------------------------------------
...(各对象完整状态明细,同 monit status)

适用场景:改完规则 reload 后想立刻看到新规则的首轮校验结果,而不是等 30 秒。

4.5 monit -B —— 无表格纯文本输出

monit -B summary

实测输出:

Monit 6.0.0 uptime: 16m
app-server                        OK                          System
sshd                              OK                          Process
fake_app                          OK                          Process
passwd_file                       OK                          File
rootfs                            OK                          Filesystem
demo_probe                        OK                          Program

-B 关闭表格与颜色渲染,输出便于 grep/awk 与日志采集;脚本里推荐 monit -B summary | grep -v OK 检查异常行。

4.6 monit -v —— 运行时配置自检

monit -v summary | head -12

实测输出:

Adding 'allow 10.89.58.0/255.255.255.0'
Adding credentials for user 'admin'
Runtime constants:
 Control file       = /etc/monitrc
 Log file           = /var/log/monit.log
 Pid file           = /var/run/monit.pid
 Id file            = /var/lib/monit/id
 State file         = /var/lib/monit/state
 Debug              = True
 Log                = True
 Use syslog         = False
 Is Daemon          = True

排障利器:确认 CLI 实际解析到的控制文件/日志/pid/id/state 路径与 allow 规则,排查"改了配置怎么没生效"类问题。

五、动作控制类命令详解

5.1 异步语义:命令下发 ≠ 立即生效

Monit 的 start/stop/restart/monitor/unmonitor 都是两段异步:CLI 把动作请求写入守护进程的动作队列并立即返回,守护进程在下一个轮询周期(本环境 set daemon 30,即 30 秒内)真正执行。期间 summary 显示 pending 过渡态。实测:

monit unmonitor all
monit summary

实测输出(下发后立即查询):

Monit 6.0.0 uptime: 12m
 Service Name                     Status                      Type
 app-server                       OK - unmonitor pending      System
 sshd                             OK - unmonitor pending      Process
 fake_app                         OK - unmonitor pending      Process
 passwd_file                      OK - unmonitor pending      File
 rootfs                           OK - unmonitor pending      Filesystem
 demo_probe                       OK - unmonitor pending      Program

等一个周期后变为最终态(实测 monit summary sshd):

Monit 6.0.0 uptime: 16m
 Service Name                     Status                      Type
 sshd                             Not monitored               Process

⚠️ 注意:这 30 秒窗口容易误判"命令没生效"。要么等周期,要么 monit validate 催一轮,要么把 set daemon 周期配小些。生产演练动作命令时务必记住这个语义。

5.2 start / stop / restart —— 进程级控制

对 Process 类对象,start/stop 执行规则里声明的 start program / stop program;restart = stop + start。实测(stop 后 sshd 真实停止,start 后恢复):

monit stop sshd && sleep 35 && systemctl is-active ssh
monit start sshd && sleep 35 && systemctl is-active ssh
monit restart sshd && sleep 35 && systemctl is-active ssh

实测输出(三条依次):

inactive
active
active

要点:

  • 动作只对 mode active 的对象有意义(passive 模式对象只告警不动作);
  • monit stop 期间该对象的测试全部挂起(状态 Not monitored 语义),不会触发"进程不存在"自愈重启——手动维护窗口用 stop/unmonitor,避免 Monit 与你抢控制权;
  • restart 常用于"改完应用配置让它周期内自动应用"的兜底。

5.3 monitor / unmonitor —— 监控开关

unmonitor 暂停某对象的全部测试与告警(配合计划内维护),monitor 恢复。实测单对象:

monit unmonitor sshd && sleep 33 && monit summary sshd
monit monitor sshd  && sleep 33 && monit summary sshd

实测输出(依次):

 Service Name                     Status                      Type
 sshd                             Not monitored               Process
 Service Name                     Status                      Type
 sshd                             OK                          Process

开关状态写入 statefile 持久化,守护进程重启后不丢(实测确认)。批量形态 monit unmonitor all / monit monitor all 见 5.1 节实测。

5.4 -g 分组批量操作

规则里声明 group <名字> 后即可分组操作。实测(core-services 组内只有 sshd):

monit -g core-services status

实测输出:

Monit 6.0.0 uptime: 16m

Process 'sshd'
  status                       OK
  monitoring status            Monitored
  ...(完整明细同 monit status sshd)

-g 可与 start/stop/restart/monitor/unmonitor/status/summary 组合,如 monit -g probes restart(本环境会同时重启 fake_app 与 demo_probe 组内对象)。分组是 Monit 版"主机组",把同一应用栈的对象(进程+文件+端口探针)编入同组,维护时一条命令整组生效。

六、配置管理类命令详解

6.1 monit -t —— 语法检查(所有变更的第一步)

monit -t

实测输出:

Control file syntax OK

配置有问题时的实测样例(include 目标无匹配文件):

Include failed -- No such file or directory '/etc/monit/conf.d/*.conf'
Monit exiting due to parsing errors in the configuration file.

规范流程:每次改规则后先 monit -t 再 monit reload。语法坏了时 reload 不会执行,且 CLI 连 summary 都会解析失败(CLI 也读控制文件),修好语法才能恢复操作。

6.2 monit reload —— 热加载配置

monit reload

实测输出:

Reinitializing monit daemon

守护进程重读 monitrc 与全部 include 文件,重建规则表;新增对象在下一轮校验时出现在 summary。reload 不中断监控轮询(区别于 systemctl restart 的进程级重启)。

6.3 monit -H —— 文件哈希计算

monit -H /etc/monit/monitrc

实测输出:

8e47203adfd25c49a8ea01725a2106a9234e9eb9ad33eaf7b0f6de8d30944657  /etc/monit/monitrc

独立小工具语义:对任意文件输出 SHA256(与 sha256sum 一致,6.0.0 的 if changed checksum 默认即此算法);不传文件则读 stdin。写文件完整性规则前,可用它先生成基线值。

6.4 monit --id / --resetid —— 实例 ID 管理

monit --id

实测输出:

Monit ID: a2684ffcdf61b436e4a46030f70c8cf4

该 ID 存于 idfile(/var/lib/monit/id),M/Monit 与事件上报以此识别主机;克隆虚拟机后两机同 ID 会串数据,执行 monit --resetid 重新生成(谨慎:M/Monit 视其为新主机)。

6.5 monit procmatch —— 匹配模式预演

写 matching 式进程规则前必做的一步:

monit procmatch "sshd"

实测输出:

The regular expression "sshd" have been matched 1 times:
/usr/sbin/sshd -D

输出正则命中的进程命令行与命中总数,把误匹配挡在规则上线前。实测教训:模式串过宽会把临时 shell 算进去(如排查命令本身包含关键字时),匹配串用完整路径最稳。

6.6 monit quit —— 守护进程停机

monit quit

实测输出:

Monit daemon with pid [2825] killed

quit 只是杀进程:本环境 systemd 单元配了 Restart=always,实测 8 秒后服务又 active(Main PID 从 2825 变为 3217)。因此 systemd 托管场景停服务的规范操作是 systemctl stop monit;monit quit 适用于 init.d/裸进程托管的场景。同理,若要 quit 后保持停机,先 systemctl stop monit。

七、Web 控制台与 XML 接口

CLI 之外,运维人员还可经浏览器与脚本使用同一控制通道(均为 2812 端口,实测详见《Monit 6.0.0 部署指南》第六节):

工具用法适用
浏览器控制台http://<绑定地址>:2812(admin/q1w2e3r4)巡检、点按启停、看服务明细与配置
XML 状态接口curl -u admin:q1w2e3r4 'http://<绑定地址>:2812/_status?format=xml'脚本聚合、自建面板数据源
无认证探测curl -s -o /dev/null -w '%{http_code}' http://<绑定地址>:2812/拨测控制台存活(401 即存活)

八、常用命令组合场景

场景 1:例行巡检(cron 或人工)

monit -B summary
monit report down

第一行看全量状态,第二行只列异常对象;两者都无输出异常即健康。

场景 2:发布/变更流程

vi /etc/monit.d/myapp.conf
monit -t && monit reload && monit validate
monit status myapp

语法检查 → 热加载 → 立即校验 → 核对明细,四步闭环。

场景 3:计划内维护窗口(防止 Monit 抢救你正要停的进程)

monit -g core-services unmonitor
# ... 执行维护 ...
monit -g core-services monitor

场景 4:应急诊断三连

monit status <异常对象>      # 细看哪个测试失败
tail -50 /var/log/monit.log  # 看 Monit 动作与事件记录
monit -v summary | head -12  # 确认 CLI 解析到的配置路径

九、常见问题(FAQ)

Q1:Cannot connect to [10.89.58.3]:2812 -- Connection refused

守护进程没在跑、控制台未配置或还在 start delay(实测延迟 15 秒内必现)。按 systemctl is-active monit → monitrc 是否有 set httpd → 稍候重试的顺序排查。

Q2:动作下发后 summary 一直 pending

正常语义:动作在下一个 set daemon 周期执行(本环境 30 秒)。急着看结果用 monit validate 催一轮。

Q3:monit summary process 报 Service 'process' not found

6.0.0 的 summary/status/monitor 等命令参数是服务名不是类型(详见第三节 💡 注意)。按类型批量操作用 -g <分组>。

Q4:改了配置没生效 / CLI 全部命令报 parsing errors

先 monit -t 修语法(include 目标为空、权限超 0700 都会导致解析失败),再 monit reload。语法坏时 CLI 自身也会解析失败,这是"连命令都跑不了"的根因。

Q5:monit quit 后服务又活了

systemd 单元 Restart=always 在 5 秒内拉起守护进程(实测)。规范停机用 systemctl stop monit。

Q6:procmatch 预演 0 命中但规则一直 Does not exist

正则写法问题(如误加锚点或匹配串与命令行不一致),用 ps -eo pid,args | grep <关键字> 核对真实命令行后修正 matching 串;务必用 monit procmatch 预演确认命中数。

十、参考链接

  1. 官方手册(CLI 与 DSL 全集):https://mmonit.com/monit/documentation/
  2. 6.0.0 Release Notes:https://mmonit.com/monit/changes/
  3. 本专栏关联篇:《Monit 6.0.0 介绍与选型指南》《Ubuntu 22.04 安装 Monit 6.0.0 指南》《Monit 6.0.0 部署指南》