Monit 服务进程监控指南

发布时间:2026/9/13 22:42:36
Monit 服务进程监控指南 一、Monit 简介Monit 是一款开源的系统和服务监控工具能够实时监测服务器的各项指标包括 CPU、内存、磁盘使用情况等尤其擅长对服务进程进行监控。当被监控的进程出现异常如进程停止、崩溃、占用资源过高时Monit 可以自动采取相应措施如重启进程、发送警报通知管理员有效保障服务器的稳定运行。目前Monit暂支持Unix OS相关系统暂不支持 Windows二、安装 Monit方法 1基于包管理工具安装一基于 OS x 系统Homebrew 是 macOS 上最常用的包管理工具可以方便地安装 Monit。安装 Monitsql代码解读复制代码snackpubsnackpubdeMacBook-Pro ~ % sudo brew install monit # 验证安装 snackpubsnackpubdeMacBook-Pro ~ % monit -V This is Monit version 5.34.4 Built with ssl, with ipv6, with compression, with pam and with large files Copyright (C) 2001-2025 Tildeslash Ltd. All Rights Reserve // brew uninstall monit二基于 CentOS 系统安装 EPEL 仓库Extra Packages for Enterprise LinuxMonit 软件包位于此仓库中arduino代码解读复制代码sudo yum install epel - release2. 安装 Monit代码解读复制代码sudo yum install monit3. 基础命令python代码解读复制代码# 查看版本 sudo monit -V # 语法检测 sudo monit -t # -I 选项的作用是让 Monit 以守护进程的方式在后台运行。 sudo monit -I # 启动monit sudo monit start all # 查看状态 sudo monit status sudo monit status all # 重新加载监视器 sudo monit reload # 启动 Monit 时使用 -d 选项来指定轮询时间设置轮询时间为 60 秒 sudo monit -d 60 -I # 查看所有服务 sudo monit summary sudo lsof -i :8081方法 2手动编译安装下载 Monit 源码访问 Monit 官方网站 下载最新版本的源码包如monit-5.32.0.tar.gz解压源码包bash代码解读复制代码tar -xzf monit-5.32.0.tar.gz cd monit-5.32.0编译和安装go代码解读复制代码./configure make sudo make install验证安装代码解读复制代码monit -V可能的错误vbnet代码解读复制代码./configure checking for SSL support... enabled checking for SSL include directory... Not found Couldnt find your SSL header files. Use --with-ssl-incl- dir option to fix this problem or disable the SSL support with --without-ssl在编译 Monit 时如果遇到SSL header files not found的错误说明系统缺少 OpenSSL 的开发头文件openssl-dev或openssl-devel。以下是解决此问题的步骤验证openssl安装代码解读复制代码brew info openssl记下 OpenSSL 的安装路径例如/usr/local/opt/openssl。配置 Monit 使用 OpenSSL在编译 Monit 时指定 OpenSSL 的头文件和库文件路径。1. 使用--with-ssl-incl-dir和--with-ssl-lib-dir运行./configure时添加以下选项javascript代码解读复制代码./configure --with-ssl-incl-dir/usr/local/opt/openssl/include \ --with-ssl-lib-dir/usr/local/opt/openssl/lib2. 禁用 SSL 支持不推荐如果你不需要 SSL 支持可以禁用 SSLbash代码解读复制代码./configure --without-ssl三、配置 Monit一主配置文件Monit 的主配置文件通常位于/etc/monitrc。打开该文件进行全局配置如设置日志文件路径、监控周期等。如果配置文件不存在可以手动创建bash代码解读复制代码sudo touch /etc/monitrc sudo chmod 600 /etc/monitrc例如修改日志文件路径为/var/log/monit.logc代码解读复制代码set logfile /var/log/monit.log设置监控周期为每 120 秒检查一次服务状态arduino代码解读复制代码set daemon 120二添加服务监控配置在主配置文件末尾或单独创建一个配置文件如/etc/monit/conf.d/目录下的文件来定义要监控的服务。以监控 Nginx 服务为例ini代码解读复制代码check process nginx with pidfile /var/run/nginx.pid start program /usr/sbin/service nginx start stop program /usr/sbin/service nginx stop if failed host 127.0.0.1 port 80 protocol http then restart if 5 restarts within 5 cycles then timeout上述配置中check process nginx with pidfile /var/run/nginx.pid定义监控名为nginx的进程通过/var/run/nginx.pid文件来识别进程 ID。start program和stop program指定启动和停止 Nginx 服务的命令。if failed host 127.0.0.1 port 80 protocol http then restart如果通过 HTTP 协议访问本地127.0.0.1:80失败则重启 Nginx 服务。if 5 restarts within 5 cycles then timeout如果在 5 个监控周期内重启了 5 次则视为超时Monit 将不再尝试重启。四、启动与管理 Monit一启动 Monit 服务sql代码解读复制代码sudo monit start三检查 Monit 状态查看 Monit 服务运行状态lua代码解读复制代码sudo monit status2. 查看 Monit 监控的服务状态css代码解读复制代码monit summary五、监控结果与警报一查看监控结果通过monit summary命令可以获取当前监控的所有服务的简要状态信息包括服务名称、状态运行、停止、异常等。也可以使用monit status命令获取更详细的监控信息如进程资源使用情况等。二设置警报通知Monit 支持通过邮件发送警报通知。在主配置文件monitrc中进行如下配置kotlin代码解读复制代码set mailserver smtp.example.com port 587 username your_emailexample.com password your_password set alert your_emailexample.com注意password是各类邮箱授权码需要自己去相关的邮箱服务器生成。在第三方客户端登录时密码框请输入授权码。可参考我的另一篇文章# 配置QQ邮箱作为SMTP服务器来发送邮件配置QQ邮箱作为SMTP服务器来发送邮件上述配置中设置了 SMTP 邮件服务器地址、端口、用户名和密码并指定将警报发送到your_emailexample.com邮箱。当被监控的服务出现异常并触发警报条件时管理员将收到包含详细异常信息的邮件通知。六、Monit设置为自启服务以下是针对Linux (systemd)和macOS (launchd)的详细配置脚本确保 Monit 开机自启并正确管理服务1. Linux (systemd) 配置(1) 创建 systemd 服务文件bash代码解读复制代码sudo vim /etc/systemd/system/monit.service填入以下内容ini代码解读复制代码[Unit] DescriptionMonit - system monitoring tool Documentationman:monit(1) Afternetwork.target [Service] Typeforking ExecStart/usr/bin/monit -c /etc/monitrc ExecStop/usr/bin/monit -c /etc/monitrc quit ExecReload/usr/bin/monit -c /etc/monitrc reload Restarton-failure RestartSec30s [Install] WantedBymulti-user.target(2) 启用并启动 Monitbash代码解读复制代码sudo systemctl daemon-reload # 重新加载配置 sudo systemctl enable monit # 开机自启 sudo systemctl start monit # 立即启动 sudo systemctl status monit # 检查状态(3) 验证日志bash代码解读复制代码journalctl -u monit -f # 实时查看日志2. macOS (launchd) 配置(1) 创建 launchd plist 文件bash代码解读复制代码sudo vim /Library/LaunchDaemons/com.monit.plist填入以下内容根据实际路径调整xml代码解读复制代码?xml version1.0 encodingUTF-8? !DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd plist version1.0 dict keyLabel/key stringcom.monit/string keyProgramArguments/key array string/usr/local/bin/monit/string string-c/string string/usr/local/etc/monitrc/string /array keyRunAtLoad/key true/ keyKeepAlive/key true/ keyStandardOutPath/key string/var/log/monit.log/string keyStandardErrorPath/key string/var/log/monit.log/string /dict /plist(2) 设置权限并加载bash代码解读复制代码sudo chmod 644 /Library/LaunchDaemons/com.monit.plist sudo launchctl load /Library/LaunchDaemons/com.monit.plist sudo launchctl start com.monit(3) 验证日志bash代码解读复制代码tail -f /var/log/monit.log3. Monit 配置文件示例确保/etc/monitrc或/usr/local/etc/monitrc包含以下内容以监控 Nginx 为例bash代码解读复制代码# 全局设置 set logfile /var/log/monit.log set pidfile /var/run/monit.pid set statefile /var/run/monit.state # 监控 Nginx check process nginx with pidfile /var/run/nginx.pid start program /usr/bin/systemctl start nginx stop program /usr/bin/systemctl stop nginx if failed host 127.0.0.1 port 80 protocol http then restart if does not exist then start # 关键确保服务不存在时自动启动4. 关键点总结系统配置文件路径自启命令日志查看Linux/etc/systemd/system/monit.servicesystemctl enable monitjournalctl -u monitmacOS/Library/LaunchDaemons/com.monit.plistlaunchctl load com.monittail -f /var/log/monit.log5. 常见问题解决Q1: Monit 启动但服务未自动拉起检查配置中是否有if does not exist then start。确保服务检查块的start program命令正确。Q2: launchd 报权限错误bash代码解读复制代码sudo chown root:wheel /Library/LaunchDaemons/com.monit.plistQ3: systemd 无法启动 Monit检查日志bash代码解读复制代码sudo journalctl -u monit --no-pager -n 50Q4: Operation not permitted我发现问题了如果配置了launchctl去启动monit就会出现 myapp failed to start (exit status 126) -- /Users/snackpub/Desktop/temp-files/jar/start.sh start: /bin/bash: /Users/snackpub/Desktop/temp-files/jar/start.sh: Operation not permittedmacOS 的安全机制阻止了脚本的执行即使直接运行脚本是正常的。这是 macOS 的安全沙盒Sandbox和隐私保护机制导致的常见问题。根本原因Gatekeeper 限制macOS 会标记从网络下载的文件包括脚本即使你chmod xlaunchd或 Monit 运行时仍可能被阻止。TCC (Transparency, Consent, and Control) 限制macOS 要求后台服务如launchd必须获得明确权限才能访问用户目录如~/Desktop。SIP (System Integrity Protection) 影响部分目录如/usr、/bin受保护但你的脚本在~/Desktop通常不受 SIP 影响。解决方案✅ 方案 1移除com.apple.quarantine属性关键步骤xattr -d com.apple.quarantine /Users/snackpub/Desktop/temp-files/jar/start.sh如果仍然不行尝试彻底清除所有扩展属性xattr -cr /Users/snackpub/Desktop/temp-files/jar/start.sh✅ 方案 2在launchd或 Monit 配置中声明完整权限如果使用launchd推荐修改/Library/LaunchDaemons/com.monit.plist添加EnableGlobbing和AbandonProcessGroupxml代码解读复制代码?xml version1.0 encodingUTF-8? !DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd plist version1.0 dict keyLabel/key stringcom.monit/string keyProgramArguments/key array string/bin/bash/string string/Users/snackpub/Desktop/temp-files/jar/start.sh/string stringstart/string /array keyRunAtLoad/key true/ keyWorkingDirectory/key string/Users/snackpub/Desktop/temp-files/jar/string keyEnableGlobbing/key true/ keyAbandonProcessGroup/key true/ keyStandardOutPath/key string/tmp/monit.log/string keyStandardErrorPath/key string/tmp/monit.err/string /dict /plist通过以上配置Monit 将在系统启动时自动运行并监控配置的所有服务如自动启动崩溃的进程。七、Monit 效果一览完整配置案例/etc/monitrc代码解读复制代码# 设置 Monit 的日志文件 set logfile /var/log/monit.log # 设置 Monit 的 PID 文件 set pidfile /var/run/monit.pid # 设置 Monit 的状态文件 set statefile /var/run/monit.state #设置轮询时间 set daemon 20 set httpd port 2812 and use address 0.0.0.0 # 允许所有 IP 地址访问若要限制访问可改为具体的 IP 地址 allow 0.0.0.0/0 # 允许所有 IP 地址访问可根据需求修改为具体的 IP 地址或 IP 段 allow admin:monit # 设置用户名和密码这里用户名是 admin密码是 monit可自行修改 # 设置 Monit 的邮件通知 set mailserver smtp.qq.com port 465 username 1498851306qq.com password 2pbddick3qhbkanjya3ic2 using tls with timeout 30 seconds set alert 1498851306qq.com # 设置邮件格式确保 from 字段和 username 一致 set mail-format { from: 1498851306qq.com subject: $SERVICE $EVENT at $DATE message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION. Yours sincerely, monit } # To monitor a specific endpoint like localhost:8081/htmlToPdf/a with Monit, # youll need to modify your check to include the full URL path in the HTTP # protocol check. Heres how you can update your configuration: # 记录启动时的 PID # PID_FILE/var/run/${APP_NAME}.pid # PID文件路径 # nohup java $JAVA_OPTS -jar $JAR_FILE $SPRING_OPTS $LOG_FILE 21 # echo $! $PID_FILE check process myapp with pidfile /var/run/myapp.pid start program /Users/snackpub/Desktop/temp-files/jar/start.sh start stop program /Users/snackpub/Desktop/temp-files/jar/start.sh stop if failed port 8081 protocol http request /htmlToPdf/a with timeout 5 seconds then restart if 5 restarts within 5 cycles then timeout if does not exist then restart group javaMonit 日志查看Monit 管理页面Monit 邮箱告警更多内容Monit官网作者吃饭艺术家链接https://juejin.cn/post/7485201976050712611来源稀土掘金著作权归作者所有。商业转载请联系作者获得授权非商业转载请注明出处。