2

I am trying to manage stuff on my server using monit. What I would like to do is to run 3 different gunicorn servers on 3 different ports.

Currently I am able to run all the servers at once for example in screen. I can launch servers by commands:

gunicorn -c app1.http_server.config app1.http_server.server:app
gunicorn -c app2.http_server.config app2.http_server.server:app
gunicorn -c app3.http_server.config app3.http_server.server:app

From what I understand of how monit works, I should monitrc file and there specify all the stuff, something like:

#set mailserver localhost
#set alert [email protected]

check process app1 with pidfile /var/run/app1.pid
    start program = "gunicorn -c app1.http_server.config app1.http_server.server:app"
    stop program  = "???"
    if failed unixsocket ??? then start
    if cpu > 50% for 5 cycles then alert

# TODO app2, app3

check system resources
    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 filesystem rootfs with path /
    if space usage > 80% then alert

I have tried to put various stuff to stop program field and same to start program, but monit is not able to launch the gunicorn server. So my question is how can I run and stop gunicorn server from monit? And what would be the gunicorn's unixsocket when I'll launch it? Could anyone provide some example that might help me to set this up?

2 Answers 2

3

I had the same problem. Monit is happier with full paths like "/virtualenv_path/bin/gunicorn". If you don't use virtualenv, just remove it wherever I placed it. The commands are quite long, but it worked that way:

check process pymonit with pidfile /path/to/pid/gunicorn.pid
  start program = "/virtualenv_path/bin/python /virtualenv_path/bin/gunicorn -c /project/path/gunicorn.conf.py /project/path/wsgi:application"
  stop program = "/usr/bin/pkill -f '/virtualenv_path/bin/python /virtualenv_path/bin/gunicorn -c /project/path/gunicorn.conf.py /project/path/wsgi:application'"
  if failed host 127.0.0.1 port 8011 protocol http then restart
  if 5 restarts within 5 cycles then alert

In your gunicorn.conf, you should have something like this:

bind = '127.0.0.1:8011'
...
pidfile = '/path/to/pid/gunicorn.pid'

in your gunicorn.conf.

2
  • I'm having trouble with /project/path/wsgi:application. I'm getting an ImportError: No module named '/project/path/wsgi' I can only get it to accept relative dot paths project.path.wsgi:application. But these don't work because the have to be relative.
    – Moose
    Apr 12, 2017 at 18:45
  • May need to set chdir = /project/path in gunicorn.conf.
    – Moose
    Apr 12, 2017 at 20:16
0

It seems like you are using monit as a startup service?

Depending on your OS, you should still create a sysvinit, systemd or upstart file for your apps, that also means it will launch on boot, and not be dependent on monit to be running and have its first tick.

This also simplifies your monit config, to just be service app1 start / stop.

If the OS doesn't have either of those systems create a shell script that sources your virtualenv properly and changes directory (not needed with gunicorns chdir though), then you have a more readable monit config.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.