简介 supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具。可以很方便的监听、启动、停止、重启一个或多个进程。用supervisor管理的进程,当一个进程意外被杀死,supervisor监听到进程死后,会自动将它重启,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。
尽量使用root来运行;使用软件安装方式来处理;不要通过pip来安装,很容易出现问题;
安装 1 2 3 4 sudo apt-get install supervisor yum install -y supervisor
配置一个内置的http可视化服务
1 2 [inet_http_server] port =127.0 .0.1 :9001
检查是否一个守护进程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@test-qingzhou-01 supervisord.d] root 4154 0.0 0.1 56144 15060 pts/0 Ss+ 13:31 0:00 /usr/bin/python /usr/bin/supervisord -nc /etc/supervisor/supervisord.conf supervisord -c /etc/supervisor/supervisord.conf ps aux |grep -v grep | grep supervisord | awk '{print $2}' |xargs kill -2 supervisorctl status 查看进程运行状态-list全部进程 supervisorctl start 进程名 启动进程 supervisorctl stop 进程名 关闭进程 supervisorctl restart 进程名 重启进程 supervisorctl update 重新载入配置文件 supervisorctl shutdown 关闭supervisord supervisorctl clear 进程名 清空进程日志 supervisorctl 进入到交互模式下。使用help 查看所有命令。 start stop restart + all 表示启动,关闭,重启所有进程。
监控当前的运行的服务
watch -n 3 supervisorctl status
supervisorctl-命令行手册
普通用户运行-supervisor
配置 系统配置位置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /etc/supervisor/supervisord.conf /etc/supervisord.conf systemctl status|stop|start|restart|enable supervisor echo_supervisord_conf > /etc/supervisord.conf [inet_http_server] ; inet (TCP) server disabled by default port=0.0.0.0:10080 ; (ip_address:port specifier, *:port for all iface) username=user ; (default is no username (open server)) password=123 ; (default is no password (open server)) minfds=65535 ; (min. avail startup file descriptors;default 1024) minprocs=65535 ; (min. avail process descriptors;default 200)
配置自己的ini
1 2 3 4 5 6 7 8 9 10 11 12 13 14 [group:qzsrv3] programs =Lobby,Gateway,Scene,Matcher,Level,DBServer[program:Login] directory =/home/teamcityagent/BuildAgent/work/5 c951883da1eba28/server/bincommand =/home/teamcityagent/BuildAgent/work/5 c951883da1eba28/server/bin/Login srv3autostart =false autorestart =unexpectedstartsecs =1 stderr_logfile =/home/teamcityagent/BuildAgent/work/5 c951883da1eba28/server/log/Login.err.logstdout_logfile =/home/teamcityagent/BuildAgent/work/5 c951883da1eba28/server/log/Login.loguser =teamcityagent
这里可以指定是否出现问题之后直接重启。
问题 1 2 3 4 5 6 7 8 9 10 11 [root@test-qingzhou-01 supervisor] Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord. [root@test-qingzhou-01 supervisor] /run/supervisor/supervisor.sock /var/lib/docker/overlay2/d6de9ac36e7b3c66fed11ddb3cf384a18932d3f8044dda5bb2247476604acd71/diff/run/supervisor.sock /var/lib/docker/overlay2/d6de9ac36e7b3c66fed11ddb3cf384a18932d3f8044dda5bb2247476604acd71/merged/run/supervisor.sock [root@test-qingzhou-01 supervisor] [root@test-qingzhou-01 supervisor] [root@test-qingzhou-01 supervisor] unlink : cannot unlink ‘/var/lib/docker/overlay2/d6de9ac36e7b3c66fed11ddb3cf384a18932d3f8044dda5bb2247476604acd71/merged/run/supervisor.sock’: No such file or directory
web 在后台能直接操作服务器启动
XML-RPC API 可以通过编写python脚本来对web做操作。
1 2 3 4 5 6 7 8 9 10 11 12 from xmlrpc.client import ServerProxyserver = ServerProxy('http://user:123@172.16.11.92:10080/RPC2' ,verbose=True ) print (server.supervisor.getState())print (server.supervisor.startAllProcesses())
supervisord-monitor 参考文档-monitor
使用 watch 命令行监视 程序是否还在跑 在Linux中,如果你想要每隔固定时间(比如3秒)执行某个指令,并且将结果实时显示在控制台上,而不使用crontab,你可以使用watch命令。watch命令可以让你周期性地执行一个程序,并全屏显示执行结果。这非常适用于周期性地查看命令输出的场景。
1 2 watch -n 秒数 要执行的指令 watch -n 3 supervisorctl status
你可以使用 supervisord
来守护你的Java程序,确保它在崩溃或退出时自动重启。以下是一个 supervisord
配置示例:
1. 创建 supervisord
配置文件 在 /etc/supervisord.d/
目录下创建一个配置文件,比如 fullnode.conf
,内容如下:
1 2 3 4 5 6 7 8 [program:lite_fullnode] directory =/data/lite_fulllnodecommand =/usr/bin/java -Xms9G -Xmx9G -XX:ReservedCodeCacheSize=256 m -XX:MetaspaceSize=256 m -XX:MaxMetaspaceSize=512 m -XX:MaxDirectMemorySize=1 G -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:gc.log -XX:+UseConcMarkSweepGC -XX:NewRatio=2 -XX:+CMSScavengeBeforeRemark -XX:+ParallelRefProcEnabled -XX:+HeapDumpOnOutOfMemoryError -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -jar FullNode.jar -c main_net_config.confautostart =true autorestart =true startsecs =10 stderr_logfile =/var/log/lite_fullnode.err.logstdout_logfile =/var/log/lite_fullnode.out.log
2. 配置项解释
directory
: 程序运行的工作目录,这里设置为 /data/lite_fulllnode
。
command
: 启动命令,这里为你的Java程序的启动命令。
autostart=true
: supervisord
启动时自动启动该程序。
autorestart=true
: 如果程序意外退出,supervisord
会自动重启它。
startsecs=10
: 程序必须在启动后的10秒内保持运行,supervisord
才会认为启动成功。
stderr_logfile
和 stdout_logfile
: 指定标准错误输出和标准输出的日志文件路径。
user=nobody
: 使用哪个用户身份来运行该程序,你可以根据需求更改为合适的用户。
3. 启动和管理 supervisord
启动 supervisord
:
1 sudo systemctl start supervisor
启动配置的程序:
1 2 3 sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start lite_fullnode
查看程序状态:
1 sudo supervisorctl status lite_fullnode
这样配置后,supervisord
将会自动启动并守护你的Java程序,并在程序意外退出时重新启动它。
在 supervisor 中,可以通过配置文件中的 stdout_logfile 和 stderr_logfile 参数来重定向标准输出和标准错误输出。具体操作如下:
1. 打开或创建 Supervisor 的配置文件(通常在 /etc/supervisord.conf 或 /etc/supervisor/conf.d/your_program.conf)。
2. 在需要配置的程序部分,添加以下内容:
[program:your_program_name] command=/path/to/your_program ; 程序执行的命令 stdout_logfile=/path/to/your_stdout.log ; 重定向标准输出的文件路径 stderr_logfile=/path/to/your_stderr.log ; 重定向标准错误输出的文件路径 stdout_logfile_maxbytes=10MB ; 可选,日志文件的最大大小 stderr_logfile_maxbytes=10MB ; 可选,错误日志文件的最大大小 stdout_logfile_backups=5 ; 可选,保留多少个日志备份 stderr_logfile_backups=5 ; 可选,错误日志保留多少个备份 autostart=true ; Supervisor 启动时自动启动程序 autorestart=true ; 程序退出时自动重启
3. 保存配置文件后,更新 Supervisor 以应用更改:
supervisorctl update
这将把指定程序的标准输出重定向到 stdout_logfile,并将标准错误重定向到 stderr_logfile。
supervisor 配置环境变量 方法 1:在 supervisord.conf 里使用 environment 指定环境变量
你可以在 supervisord.conf 的 [program] 配置块中,使用 environment 选项来定义环境变量。例如:
1 2 3 4 5 6 7 [program:my_service] command =/usr/bin/python3 /path/to/script.pyautostart =true autorestart =true stderr_logfile =/var/log/my_service.err.logstdout_logfile =/var/log/my_service.out.logenvironment =ENV_VAR1="value1" ,ENV_VAR2="value2"
说明: • environment 后面的变量使用 逗号分隔。 • 变量值 需要加双引号(特别是如果值包含空格)。
参考