Monit 无法重新启动 nginx

Monit 无法重新启动 nginx

我在 Ubuntu 12.04 服务器上启动并运行了 monit,以监控 nginx,但我无法让它再次成功启动 nginx。我的设置如下:monit 配置文件为:

-rwx------ 1 root root 10329 2015-07-31 09:39 /etc/monit/monitrc

并且 monitrc 文件的内容(未注释)是

set daemon  60
set logfile /var/www/apps/myapp/log/monit.log

set httpd port 2812
  use address mysite.com       
  allow username:password 'password'

check system mysite.com
  if loadavg (1min) > 4 then alert
  if loadavg (5min) > 2 then alert
  if memory usage > 75% then alert
  if cpu usage (user) > 70% then alert
  if cpu usage (system) > 30% then alert
  if cpu usage (wait) > 20% then alert

check process nginx with pidfile /opt/nginx/logs/nginx.pid
  start program = "/etc/init.d/nginx restart; touch /var/www/apps/myapp/tmp/restart.txt"
  stop program = "/etc/init.d/nginx stop"

我正在用 开始监控sudo monit

如果我终止 nginx 然后等待 monit 尝试启动它,我会在日志文件中看到以下内容:

[UTC Jul 31 09:39:26] info     : Reinitializing monit daemon
[UTC Jul 31 09:39:26] info     : Starting monit HTTP server at [mysite.com:2812]
[UTC Jul 31 09:39:26] info     : monit HTTP server started
[UTC Jul 31 09:39:26] info     : 'myhost.mydomain.tld' Monit reloaded
[UTC Jul 31 09:40:26] error    : 'nginx' process is not running
[UTC Jul 31 09:40:26] info     : 'nginx' trying to restart
[UTC Jul 31 09:40:26] info     : 'nginx' start: /etc/init.d/nginx
[UTC Jul 31 09:40:56] error    : 'nginx' failed to start

当我在终端中自己运行 nginx restart 命令时,它运行正常:

sudo /etc/init.d/nginx restart; touch /var/www/apps/myapp/tmp/restart.txt

这样就可以正常重新启动它(该touch命令是重新加载在 Passenger 下运行的 ruby​​ 应用程序 - 触摸重启文件会触发应用程序的重启。)

从 monit 配置中的行中删除touch命令没有任何区别。当我尝试通过 monit 的 Web 界面重新启动 nginx 时,也失败了。

我认为如果我能以某种方式将 STDERR 和 STDOUT 流放入日志文件,当 monit 尝试启动 nginx 时,这至少会有所帮助。

有什么建议么?

答案1

monit 不接受带有start program/ 的shell 脚本stop program。您需要这样写:

check process nginx with pidfile /opt/nginx/logs/nginx.pid
  start program = "/bin/sh -c '/etc/init.d/nginx restart; touch /var/www/apps/myapp/tmp/restart.txt'"
  stop program = "/etc/init.d/nginx stop"

相关内容