0

I've got monit up and running on an Ubuntu 12.04 server, to monitor nginx, but I can't get it to successfully start nginx up again. My setup is thus: the monit config file is:

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

and the content (non commented) of the monitrc file is

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"

I'm starting monit with sudo monit.

If I kill nginx and then wait for monit to try to start it, I see this in the log file:

[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

When I run the nginx restart command myself in the terminal then it works fine:

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

this restarts it fine (the touch command is to reload a ruby app which is running under Passenger - touching the restart file triggers the restart of the app.)

Taking the touch command out of the line in the monit config doesn't make any difference. When I try to restart nginx via monit's web interface, that also fails.

I think that if I could somehow get the STDERR and STDOUT streams into the log file as well, when monit tries to start nginx, that would help at least.

Any suggestions?

Max Williams
  • 237
  • 1
  • 3
  • 14
  • 1
    monit relys on PID file `/opt/nginx/logs/nginx.pid` to know whether or not the process to be watched (`nginx`) is alive and running. By running `/etc/init.d/nginx restart` does `nginx` certainly create `nginx.pid` with its PID in the content? – yaegashi Jul 31 '15 at 10:17
  • yes - after starting nginx manually, i have `-r-------- 1 root root 5 2015-07-31 10:18 /opt/nginx/logs/nginx.pid` – Max Williams Jul 31 '15 at 10:24

1 Answers1

1

monit doesn't accept shell scripts with start program/stop program. You need to write like this:

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"
yaegashi
  • 821
  • 4
  • 10