使用 Monit 监控 Rake Task

Monit

Monit是一个跨平台的用来监控Unix/linux系统(比如Linux、BSD、OSX、Solaris)的工具。Monit特别易于安装,而且非常轻量级(只有500KB大小),并且不依赖任何第三方程序、插件或者库。

Monit可以监控服务器进程状态、HTTP/TCP状态码、服务器资源变化、文件系统变动等等,根据这些变化,可以设定邮件报警、重启进程或服务。易于安装、轻量级的实现以及强大的功能,让Monit成为一个理想的后备监控工具。

Monit 常用命令

1
2
3
4
5
6
7
8
9
10
11
monit -t # 配置文件检测
monit # 启动monit daemon
monit -c /var/monit/monitrc # 启动monit daemon时指定配置文件
monit reload # 当更新了配置文件需要重载
monit status # 查看所有服务状态
monit status nginx # 查看nginx服务状态
monit stop all # 停止所有服务
monit stop nginx # 停止nginx服务
monit start all # 启动所有服务
monit start nginx # 启动nginx服务
monit -V # 查看版本

Rake Task

Rake Task 是 Rails 执行后台任务一个工具,可以执行一些定时或者轮询的后台任务。

如何配置 monit 来监控 rake task

1
2
3
4
5
6
check process xxx_task
    matching "xxx_task"
    start program = "/bin/bash -l -c 'source /home/deploy/.bashrc && cd /home/deploy/apps/xxx_project/current/ && RAILS_ENV=production /home/deploy/.rbenv/shims/bundle exec rake xxx_task &'"
    stop program  = "/bin/bash -l -c 'pgrep -f xxx_task | xargs kill -9'"
    if memory usage > 50% then restart
    if 9 restarts within 10 cycles then timeout

解释

  • source /home/deploy/.bashrc 用来加载环境变量。因为系统使用了 rbenv,所以必须先加载这个
  • pgrep -f xxx_task | xargs kill -9 杀死所有的这个任务主进程和子进程

评论