Supervisor是一个C/S系统,它能够在类UNIX系统上控制系统进程,由python编写,提供了大量的功能来实现对进程的管理。python
sudo pip install supervisor
安装完成 supervisor 以后,可使用 “echo_supervisord_conf” 命令来生成样例配置文件linux
echo_supervisord_conf
默认 supervisor 会使用 /etc/supervisord.conf 做为默认配置文件。web
首先写个小程序来模拟一个服务程序,以下
myserver.sh小程序
#!/bin/sh while true do date sleep 5 done
修改配置文件 /etc/supervisord.conf ,内容以下浏览器
[supervisord]
nodaemon=true [program:myserver] command=/home/kongxx/test/myserver.sh
supervisord -c /etc/supervisord.conf
运行上面的程序便可启动supervisor服务,此时会在当前目录下生成一个日志文件 supervisord.log。bash
此时咱们使用 “ps -ef | grep myserver” 找到上面的服务进程,而后kill掉这个进程。此时就会看到日志中 supervisor 会启动一个新的myserver进程。socket
对于上面的例子咱们只能启动一个服务,却不能管理这些配置的服务,下面就看看怎样管理服务。ui
仍是使用上面myserver.sh程序。url
/etc/supervisord.confspa
[inet_http_server] ; inet (TCP) server disabled by default
port = *:9999 ; (ip_address:port specifier, *:port for all iface) username = admin ; (default is no username (open server)) password = Letmein ; (default is no password (open server)) [supervisord] nodaemon = false [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl = http://127.0.0.1:9999 ; use an http:// url to specify an inet socket username = admin ; should be same as http_username if set password = Letmein ; should be same as http_password if set prompt = mysupervisor ; cmd line prompt (default "supervisor") [program:myserver] command = /home/kongxx/test/myserver.sh redirect_stderr = true stdout_logfile = /tmp/myserver.log
supervisord -c /etc/supervisord.conf
supervisord : 启动supervisor supervisorctl reload :修改完配置文件后从新启动supervisor supervisorctl update:根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启 supervisorctl status :查看supervisor监管的进程状态 supervisorctl start 进程名 :启动XXX进程 supervisorctl stop 进程名 :中止XXX进程 supervisorctl stop all:中止所有进程,注:start、restart、stop都不会载入最新的配置文件
supervisorctl也能够不带任何参数,此时便可进入supervisor的管理命令行接口,以下:
$ supervisorctl
myserver RUNNING pid 15297, uptime 0:00:27 mysupervisor> ? default commands (type help <topic>): ===================================== add exit open reload restart start tail avail fg pid remove shutdown status update clear maintail quit reread signal stop version mysupervisor>
supervisorctl -s http://<ip>:9999 -u admin -p Letmein status myserver
可使用浏览器访问 http://:9999 来经过web接口管理服务。
转载请以连接形式标明本文地址
遇到的问题
解决方案:
首先你要让文件有可以执行的权限,好比你的文件是a.sh那么你能够
chmod +x a.sh
而后运行文件就能够了
./a.sh
这样运行是a.sh在当前工做目录,若是文件没在当前目录,那么就须要用绝对路径来执行,好比
/opt/a.sh
/opt/test/a.sh
解决方案:重启进程