Linux下Supervisor安装和使用(以后台运行SpringBoot的jar文件为例)

2020-12-23 1875点热度 1人点赞 0条评论

1. Supervisor介绍

Supervisor是用一个基于Python的通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启,让程序跟随开机启动。只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。我们可以把一个开发完成的普通程序(比如SpringBoot开发的jar文件)轻松转换成后台运行的程序。

2. Supervisor安装

以CentOS7为例
#安装epel
yum -y install epel-release
#安装supervisor
yum -y install supervisor

#开机自启动
systemctl enable supervisord
#启动supervisord服务 (supervisord -c /etc/supervisord.conf
systemctl start supervisord
后续运维的操作
# 查看supervisord服务状态
systemctl status supervisord

# 查看是否存在supervisord进程
ps -ef|grep supervisord 

#查看进程中的任务
supervisorctl -c /etc/supervisord.conf

3. Supervisor的配置

我们以新建一个SpringBoot生成的jar文件 app1.jar为例,假设给这个程序起名为app1(在supervisor中起个名字方便管理)

/etc/supervisord.d/下新建app1.ini文件,vi /etc/supervisord.d/app1.ini 配置如下

[program:app1]
;;java运行使用到的参数自行调整,如果修改了这些命令,需要使用supervisorctl reload来使其生效
command=java -Xms2048M -Xmx4096M -Dconf.home="/opt/app1/" -Dlog4j.configuration="file:/opt/app1/log4j.properties"  -jar /opt/app1/app1.jar --spring.config.location=/opt/app1/

;; 以下为可选参数
;;directory=/opt/bin
;;autostart=true 
;;autorestart=false 
;;stderr_logfile=/tmp/test_stderr.log 
;;stdout_logfile=/tmp/test_stdout.log 
;;user = test

4. 后续运维操作(supervisorctl)

#以下例子的app1就是/etc/supervisord.d/app1.ini 里的[program:app1]的app这个进程

#重新加载配置文件
supervisorctl reload
#停止某一个进程
supervisorctl stop app1
#启动某个进程
supervisorctl start app1
#重启某个进程
supervisorctl restart app1
#查看进程状态
supervisorctl status app1
#停止全部进程 start、restart、stop 都不会载入最新的配置文件
supervisorctl stop all
#载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程
supervisorctl reload
#根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。
supervisorctl update

#也可以先输入supervisorctl,进入交互命名后,直接输入status、restart app1等等命令

更多信息可查看官网 http://supervisord.org

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论