supervisor能守护前台进程,实现服务的管理,包括服务开机自启动,关机自停止,服务优先级,日志管理,以及进程崩溃自动重启等,借助supervisor我们能更方便、更智能地对线上环境java服务的管理。
本文基于CentOS7系统,安装supervisor软件包
yum install supervisor -y
因为supervisor的子配置文件是位于/etc/supervisord.d/*.ini,所以创建需要守护的java服务配置文件
vi /etc/supervisord.d/java-.ini
[program:projectname]
directory = /usr/local/projectname
environment = JAVA_HOME="/usr/local/jdk1.8.0_201",PATH="$JAVA_HOME/bin:$PATH:/root/soft/node/bin",CLASSPATH=".:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar"
command = /usr/local/jdk/bin/java -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Djava.ext.dirs=/usr/local/jdk1.8.0_201/jre/lib/ext:/usr/local/jdk
priority = 10
numprocs = 1
autostart = true
autorestart = true
startretries = 50
stopwaitsecs = 25
startsecs = 15
exitcodes = 0
user = root
group = root
stdout_logfile = /var/log/projectname.log
stderr_logfile = /var/log/projectname.log
redirect_stderr = true
stdout_logfile_maxbytes = 5MB
stdout_logfile_backups = 10
设置supervisor服务本身开机自启动
systemctl enable supervisor
启动supervisor服务
systemctl start supervisor
启动/停止/重启/查看守护的java服务
supervisorctl start/stop/reload/status projectname