Skip to main content
 首页 » 编程设计

python之使用 monit 运行和停止 gunicorn 服务器

2024年10月25日10小虾米

我正在尝试使用 monit 管理服务器上的内容。我想做的是在 3 个不同的端口上运行 3 个不同的 gunicorn 服务器。

目前我可以同时运行所有服务器,例如在屏幕上。我可以通过命令启动服务器:

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 

根据我对 monit 工作原理的理解,我应该在 monitrc 文件中指定所有内容,例如:

#set mailserver localhost 
#set alert myemail@gmail.com 
 
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 

我尝试将各种东西放入停止程序字段和启动程序,但 monit 无法启动 gunicorn 服务器。所以我的问题是如何从 monit 运行和停止 gunicorn 服务器?当我启动它时,gunicorn 的 unixsocket 会是什么?谁能提供一些可以帮助我进行设置的示例?

请您参考如下方法:

我遇到了同样的问题。 Monit 更喜欢“/virtualenv_path/bin/gunicorn”这样的完整路径。如果您不使用 virtualenv,只需将其删除即可。命令很长,但它是这样工作的:

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 

在你的 gunicorn.conf 中,你应该有这样的东西:

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

在你的 gunicorn.conf 中。