supervisor 管理后台进程

sudo easy_install supervisor

它的配置文件使用;做为注释符

另外有点类似于nginx

使用supervisord -c supervisord.conf这样的方式在后台执行

使用supervisorctl进行命令行的控制,这个有点像redis

看了pycon上的视频,发现自已水平好菜,老外好牛,差距大,还没想到什么方法,只有多写代码来着。

 

以下reference:

http://lincolnloop.com/blog/2010/jun/24/automatically-running-supervisord-startup/

Automatically Running Supervisord on Startup

I really like supervisord for long-running process management. It is Python, so it is easy to install and it is easy to script with tools like Fabric. Lately I’ve been deploying smaller Django/WSGI sites with Green Unicorn behind Nginx proxied over a Unix socket which makes for a nice little setup on shared hosting and RAM starved VPSes.

After installing supervisord with something like sudo easy_install supervisor, you’ll want to make sure it runs after you reboot your machine (and gets restarted in case it crashes). If you are on a Linux system with upstart (and have root access), you can bypass the ugly init scripts and use this simple file placed in /etc/init/supervisord.conf to manage your supervisord process:

description     "supervisord"

start on runlevel [2345]
stop on runlevel [!2345]

respawn

exec /usr/local/bin/supervisord --nodaemon --configuration /etc/supervisord.conf 

Now that it is in upstart, you can start it with the command sudo start supervisord.

If you are on shared hosting, you probably won’t have permissions to do this, but if you have access tocron, you can make sure it gets started in case the server reboots by adding this to your cron jobs:

@reboot	/my/path/to/supervisord -c /my/path/to/supervisord.conf 2>&1

In the event supervisord crashes or is killed off by a sysadmin, you’re out of luck. If this happens, you’ll want to look into having a cron job periodically poll the process pid to see if it is still alive and restart it if it is not. I haven’t needed this (yet), so that is left as an exercise for the reader :)

COMMENTS

  • June 24, 2010 at 12:56 p.m.  #
    Simon Luijk responded:

    I am using daemontools to monitor gunicorn. When I update my code I send gunicorn a SIGHUP to gracefully reload the code. This creates all sorts of problems. As expected gunicorn starts up a new process and then shuts the original process down. At which point daemontools tries to start gunicorn as it thinks gunicorn has died. I have a tempary hack which I’ll spare you from.

    I have been looking for an alternative and supervisord is high on the list but I have not been able to find anything conclusive on sending signals to monitored processes. What is your experience in getting gunicorn to gracefully reload code with your setup?

  • June 25, 2010 at 4:48 a.m.  #
    ZeissS chimed in with:

    @Simon: You could use the supervisorctl tool to signal supervisord to restart the process.

  • June 28, 2010 at 1:57 a.m.  #
    Simon Luijk chimed in with:

    @ZeissS That would stop and start the process and not gracefully reload the code.

    I guess my question is does supervisord get its knickers in a twist if I “kill -HUP `cat /tmp/gunicorn.pid`” like daemontools does?

    Seeing that you can’t give supervisord a pid file for a process, I guess the answer is yes.

GOT SOMETHING TO SAY?

 

 

 

 

以下reference:http://blog.chinaunix.net/u2/87077/showart_2300645.html

使用supervisor监控进程
 
 
在linux下监控进程,可以使用inittab,最近找到了supervisor,也很好用,记录一下:
1、系统要安装python,并安装与之对应的setuptools, 下载地址在此
2、安装:
# sh setuptoolsxxxx.egg
3、安装supervisor, 下载地址在此,解压缩后
# python setup.py install
这就ok了,然后执行
# echo_supervisord_conf > /etc/supervisord.conf
修改/etc/supervisord.conf文件,加入你要监控的进程,里面的注释很详细,举个简单的例子:
这是一段要监控的进程的描述信息,添加到这个文件的末尾就好了:
[program:meta.txn.recover.on.error]
command=/cas/bin/meta.txn.recover.on.error ; 被监控的进程路径
numprocs=1                    ; 启动几个进程
directory=/cas/bin                ; 执行前要不要先cd到目录去,一般不用
autostart=true                ; 随着supervisord的启动而启动
autorestart=true              ; 自动重启。。当然要选上了
startretries=10               ; 启动失败时的最多重试次数
exitcodes=0                 ; 正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)
stopsignal=KILL               ; 用来杀死进程的信号
stopwaitsecs=10               ; 发送SIGKILL前的等待时间
redirect_stderr=true          ; 重定向stderr到stdout
为了节省空间,注释的内容就不贴出来了。
执行
# supervisord -n
能在控制台看到监控进程的输出:
2010-08-17 10:26:07,467 INFO supervisord started with pid 943
2010-08-17 10:26:08,469 INFO spawned: 'meta.txn.recover.on.error' with pid 1009
2010-08-17 10:26:09,876 INFO success: meta.txn.recover.on.error entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2010-08-17 10:26:48,442 INFO exited: meta.txn.recover.on.error (terminated by SIGKILL; not expected)
2010-08-17 10:26:49,444 INFO spawned: 'meta.txn.recover.on.error' with pid 2427
2010-08-17 10:26:50,487 INFO success: meta.txn.recover.on.error entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
黑体的地方是我用kill -9杀掉进程后出来的,看到supervisor检测到进程退出后又再次启动了进程。
不带参数运行supervisord是以daemon方式运行。
把supervisord加入到开机启动项里就可以完成监控进程的功能了。


【注意】:当supervisord以非daemon方式运行时,杀掉supervisord后,被监控的进程也退出了。
而以daemon方式运行,杀掉supervisord对被监控进程无影响。

 

 

supervisor 管理后台进程

文章分类:Python编程

参考这篇文章 

加上一些notes: 

1 生成默认conf文件: echo_supervisord_conf > ... 

2 启动:superviserd -c 配置文件path 
     eg:supervised -c t.ini 
     配置文件ini格式 

3 控制:supervisorctrl -i 
     进入交互模式 

用 Supervisor 管理后台守护进程

一些虚拟主机支持跑后台守护程序, 比如 Webfaction.

在虚拟主机中管理后台守护程序没有 VPS 那么方便, VPS 是可以使用 root 权限的, 但虚拟主机则不能.

要更方便的管理后台进程, 通常需要借助一些辅助工具. 常用的管理工具有 runitdaemontools 以及 Supervisor. 其中以 Supervisor 最为易用, 功能也很完善.

安装 Supervisor

Supervisor 是一个 Python 程序, 按照 官方文档安装 就可以了.

稍加注意的是, 虚拟主机用户需要安装到他的用户目录:

easy_install supervisor --prefix=$HOME

安装完成后, 确保 $HOME/bin 目录在 $PATH 环境变量中并已经生效.

使用方法: 以 Zine 为例

Zine 可以作为后台守护程序运行, 通过 Supervisor 进行 启动/重启/失败自动重启 等控制.

Supervisor 有两个可执行程序 — supervisord 和 supervisorctl:

  • supervisord 是后台管理服务器, 用来依据配置文件的策略管理后台守护进程;
  • supervisorctl 用于管理员向后台管理程序发送 启动/重启/停止 等指令;

它们之间的关系就相当于 Apache 的 httpd 和 apachectl.

创建工作目录和配置文件

首先需要创建一个 Supervisor 的工作目录, 如: $HOME/deploy/supervisor/, 用来存放错误日志输出, pid 文件, socket 文件, 配置文件等.

接着是创建 配置文件, 配置文件用来指示 Supervisor 有哪些进程需要管理, 以及管理策略.

我们把配置文件命名为 supervisord.conf (也可以是其它任何文件名, 但 Supervisor 默认自动在当前目录查找该文件, 用 supervisord.conf 会为以后管理提供方便):

[supervisord]
logfile = %(here)s/supervisord.log
loglevel = warn
pidfile = %(here)s/supervisord.pid
directory = %(here)s/
childlogdir = %(here)s/childlog

[unix_http_server]
file = <YOU-HOME-PATH>/deploy/supervisor/supervisord.sock

[rpcinterface:supervisor]
; This section is always necessary because supervisor uses RPC internally.
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
; Must match settings in 'unix_http_server'
serverurl = unix:///<YOU-HOME-PATH>/deploy/supervisor/supervisord.sock

[program:blog]
command = <YOU-HOME-PATH>/deploy/zine/bin/python /home/yospaly/deploy/zine/scripts/server -a 127.0.0.1 --no-reloader --no-debugger --threaded -p 6666 -I <YOU-HOME-PATH>/deploy/blog/

[program:blog2]
command = <YOU-HOME-PATH>/deploy/zine/bin/python /home/yospaly/deploy/zine/scripts/server -a 127.0.0.1 --no-reloader --no-debugger --threaded -p 8888 -I <YOU-HOME-PATH>/deploy/blog2/
  • 配置文件的第一段用来配置 supervisord 后台管理服务器一些输出文件的路径;
  • 第二段用来打开 supervisord 内部的 Socket 服务, 以便接受和处理来自控制程序 (supervisorctl) 请求;
  • 第三段是必备的, 详见注释;
  • 第四段告诉控制程序 (supervisorctl) 通过什么途径和 supervisord 通信;

Note

Webfaction 的用户不能随意指定 Zine 的端口号, 要使用在 Webfaction 控制面板添加新的 “Custom app (listening on port)” 后获得的一个特定端口号.

使用 supervisorctl 进行控制

在 supervisord.conf 所在的工作目录执行 supervisord 运行后台管理服务器, 如果运行失败请查看工作目录下的错误日志;

supervisord 跑起来后, supervisorctl 就可以方便的手工管理守护程序了:

supervisorctl start all
supervisorctl stop blog
supervisorctl restart blog2

结束语

Supervisor 是一个易用, 又不失强大的工具, 这里只涉及了 Supervisor 最基本的使用, 还有很多更加高级和有用的功能未能覆盖, 可以进一步参考 Supervisor 官方文档.

2009-05-14, Thursday 17:42 PM |  0 comments 0 pingbacks | Tags: 

 

你可能感兴趣的:(super)