linux下使用supervisor监控应用程序

来源:互联网 发布:html网站源码 编辑:程序博客网 时间:2024/04/29 19:47

1 应用场景

应用程序需要24小时不间断运行。这时可使用supervisor监控应用程序的进程。当发生应用程序内部错误退出、进程被杀死等情况时,自动重启应用程序。

2 supervisor

supervisor由python写成, 简单好用。官方网站 http://supervisord.org,上面有详细的指南文档。

3 安装supervisor


1)安装 setuptools
$sudo apt-get install python-setuptools

2)  使用easy_install安装 supervisor
$sudo easy_install supervisor

安装完成后出现:
/usr/bin/supervisord --supervisor服务守护进程
/usr/bin/supervisorctl--supervisor服务控制程序

4 配置文件

以下命令可以将配置文件示例打印到控制台:
$ echo_supervisord_conf
若要生成配置文件,使用命令:
$ sudo echo_supervisord_conf > /etc/supervisord.conf
或者在当前目录生成:
$ echo_supervisord_conf > supervisord.conf

--------------------------------

Notice: 如何停止子进程

场景:如果supervisord.conf中配置的command是执行一个bash,而bash里执行java,那么当使用supervisorctl stop [programname]停止程序时,只有上层进程被停止,而java进程没有被停止。

解决办法:

在配置文件中设置:

stopasgroup=true               
killasgroup=true
 

---------------------------------

5 配置要运行的程序

在上面生成的配置文件后,追加:

[plain] view plaincopy
  1. [program:helloworld]  
  2. command=./helloworld              ;执行命令  
  3. process_name=%(program_name)s  
  4. autostart=true                   ; 程序是否随supervisor启动而启动  
  5. autorestart=true                 ;程序停止时,是否自动重启  
  6. startsecs=10  

6 启动supervisor

$ supervisord -c supervisord.conf


7 使用supervisorctl管理程序

1)开启/停止某个程序
# supervisorctl [start | stop] [program名称]      //在supervisord.conf中定义的

2)查看进程状态
$supervisorctl status

8 通过web方式查看状态

配置文件中配置:

[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)

那么可以从浏览器通过访问 localhost:9001来查看进程状态


9 添加supervisord为Linux系统服务,开机自动启动



1)将supervisord.conf拷贝到 /etc 目录下

2)启动脚本 supervisord.sh  (centos/redhat)

[plain] view plaincopy
  1. #!/bin/sh  
  2. #  
  3. # /etc/rc.d/init.d/supervisord  
  4. #  
  5. # Supervisor is a client/server system that  
  6. # allows its users to monitor and control a  
  7. # number of processes on UNIX-like operating  
  8. # systems.  
  9. #  
  10. # chkconfig: - 64 36  
  11. # description: Supervisor Server  
  12. # processname: supervisord  
  13.   
  14. # Source init functions  
  15. . /etc/rc.d/init.d/functions  
  16.   
  17. prog="supervisord"  
  18.   
  19. prog_bin="/usr/local/bin/supervisord"  
  20. PIDFILE="/tmp/supervisord.pid"  
  21.   
  22. start()  
  23. {  
  24.         echo -n $"Starting $prog: "  
  25.   
  26. # Source init functions  
  27. . /etc/rc.d/init.d/functions  
  28.   
  29. prog="supervisord"  
  30.   
  31. prog_bin="/usr/local/bin/supervisord"  
  32. PIDFILE="/tmp/supervisord.pid"start()  
  33. {  
  34.         echo -n $"Starting $prog: "  
  35.         daemon $prog_bin --pidfile $PIDFILE  
  36.         [ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog startup"  
  37.         echo  
  38. }  
  39.   
  40. stop()  
  41. {  
  42.         echo -n $"Shutting down $prog: "  
  43.         [ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"  
  44.         echo  
  45. }  
  46.   
  47. case "$1" in  
  48.   start)  
  49.     start  
  50.   ;;  
  51.   stop)  
  52.     stop  
  53.   ;;  
  54.   status)  
  55.         status $prog  
  56.   ;;  
  57.   restart)  
  58.     stop  
  59.     start  
  60.   ;;  
  61.   *)  
  62.     echo "Usage: $0 {start|stop|restart|status}"  
  63.   ;;  
  64.   
  65. esac  


3)添加为系统服务

# mv supervisord.sh  /etc/init.d/supervisord
# chkconfig --add  supervisord
# chkconfig --level 345 supervisord on

4) 开启/停止服务

# service supervisord [start | stop]

转自:http://blog.csdn.net/heyjackie/article/details/12995187

参考材料:

http://supervisord.org/running.html
http://serverfault.com/questions/96499/how-to-automatically-start-supervisord-on-linux-ubuntu
0 0