最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Linux下怎样用Supervisor管理Springboot进程
时间:2026-07-22 17:25:48 编辑:袖梨 来源:一聚教程网
Supervisor简介
Supervisor是一个C/S架构进程管理工具,通过它可以监控和控制其他的进程,同时它自身提供了一个WebUI,可以在WebUI进行start、stop、restart操作。由Supervisor管理的进程,都是它的子进程。

Supervisor的服务器端称为supervisord,主要负责在启动自身时启动管理的子进程,响应客户端的命令,重启崩溃或退出的子进程,记录子进程stdout和stderr输出,生成和处理子进程生命周期中的事件。可以在一个配置文件中配置相关参数,包括Supervisord自身的状态,其管理的各个子进程的相关属性。
Supervisor的客户端称为supervisorctl,它提供了一个类shell的接口(即命令行)来使用supervisord服务端提供的功能。通过supervisorctl,用户可以连接到supervisord服务器进程,获得服务器进程控制的子进程的状态,启动和停止子进程,获得正在运行的进程列表。客户端通过Unix域套接字或者TCP套接字与服务端进行通信,服务器端具有身份凭证认证机制,可以有效提升安全性。
当客户端和服务器位于同一台机器上时,客户端与服务器共用同一个配置文件,通过不同标签来区分两者的配置。
部署
安装Supervisor
Supervisor由Python开发,所以除了yum方式外也可以通过pip进行安装。
yum安装
yum install -y epel-release && yum install -y supervisor
pip安装
pip install supervisor
安装jdk
由于是Springboot服务所以还需要安装jdk。
yum install java-1.8.0-openjdk
Supervisor配置文件
在etc目录下生成Supervisor配置文件。
echo_supervisord_conf > /etc/supervisord.conf
完整配置文件如下,也可以根据以下内容直接创建配置文件。
; Sample supervisor config file.;; For more information on the config file, please see:; http://supervisord.org/configuration.html;; Notes:; - Shell expansion ("~" or "$HOME") is not supported. Environment; variables can be expanded using this syntax: "%(ENV_HOME)s".; - Quotes around values are not supported, except in the case of; the environment= options as shown below.; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".; - Command will be truncated if it looks like a config file comment, e.g.; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".;; Warning:; Paths throughout this example file use /tmp because it is available on most; systems. You will likely need to change these to locations more appropriate; for your system. Some systems periodically delete older files in /tmp.; Notably, if the socket file defined in the [unix_http_server] section below; is deleted, supervisorctl will be unable to connect to supervisord.[unix_http_server]file=/tmp/supervisor.sock ; the path to the socket file;chmod=0700 ; socket file mode (default 0700);chown=nobody:nogroup ; socket file uid:gid owner;username=user ; default is no username (open server);password=123 ; default is no password (open server); Security Warning:; The inet HTTP server is not enabled by default. The inet HTTP server is; enabled by uncommenting the [inet_http_server] section below. The inet; HTTP server is intended for use within a trusted environment only. It; should only be bound to localhost or only accessible from within an; isolated, trusted network. The inet HTTP server does not support any; form of encryption. The inet HTTP server does not use authentication; by default (see the username= and password= options to add authentication).; Never expose the inet HTTP server to the public internet.;[inet_http_server] ; inet (TCP) server disabled by default;port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface;username=user ; default is no username (open server);password=123 ; default is no password (open server)[supervisord]logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.loglogfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MBlogfile_backups=10 ; # of main logfile backups; 0 means none, default 10loglevel=info ; log level; default info; others: debug,warn,tracepidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pidnodaemon=false ; start in foreground if true; default falsesilent=false ; no logs to stdout if true; default falseminfds=1024 ; min. avail startup file descriptors; default 1024minprocs=200 ; min. avail process descriptors;default 200;umask=022 ; process file creation umask; default 022;user=supervisord ; setuid to this UNIX account at startup; recommended if root;identifier=supervisor ; supervisord identifier, default is 'supervisor';directory=/tmp ; default is not to cd during start;nocleanup=true ; don't clean up tempfiles at start; default false;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP;environment=KEY="value" ; key value pairs to add to environment;strip_ansi=false ; strip ansi escape codes in logs; def. false; The rpcinterface:supervisor section must remain in the config file for; RPC (supervisorctl/web interface) to work. Additional interfaces may be; added by defining them in separate [rpcinterface:x] sections.[rpcinterface:supervisor]supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface; The supervisorctl section configures how supervisorctl will connect to; supervisord. configure it match the settings in either the unix_http_server; or inet_http_server section.[supervisorctl]serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket;username=chris ; should be same as in [*_http_server] if set;password=123 ; should be same as in [*_http_server] if set;prompt=mysupervisor ; cmd line prompt (default "supervisor");history_file=~/.sc_history ; use readline history if available; The sample program section below shows all possible program subsection values.; Create one or more 'real' program: sections to be able to control them under; supervisor.;[program:theprogramname];command=/bin/cat ; the program (relative uses PATH, can take args);process_name=%(program_name)s ; process_name expr (default %(program_name)s);numprocs=1 ; number of processes copies to start (def 1);directory=/tmp ; directory to cwd to before exec (def no cwd);umask=022 ; umask for process (default None);priority=999 ; the relative start priority (default 999);autostart=true ; start at supervisord start (default: true);startsecs=1 ; # of secs prog must stay up to be running (def. 1);startretries=3 ; max # of serial start failures when starting (default 3);autorestart=unexpected ; when to restart if exited after running (def: unexpected);exitcodes=0 ; 'expected' exit codes used with autorestart (default 0);stopsignal=QUIT ; signal used to kill process (default TERM);stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10);stopasgroup=false ; send stop signal to the UNIX process group (default false);killasgroup=false ; SIGKILL the UNIX process group (def false);user=chrism ; setuid to this UNIX account to run the program;redirect_stderr=true ; redirect proc stderr to stdout (default false);stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB);stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10);stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0);stdout_events_enabled=false ; emit events on stdout writes (default false);stdout_syslog=false ; send stdout to syslog with process name (default false);stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB);stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10);stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0);stderr_events_enabled=false ; emit events on stderr writes (default false);stderr_syslog=false ; send stderr to syslog with process name (default false);environment=A="1",B="2" ; process environment additions (def no adds);serverurl=AUTO ; override serverurl computation (childutils); The sample eventlistener section below shows all possible eventlistener; subsection values. Create one or more 'real' eventlistener: sections to be; able to handle event notifications sent by supervisord.;[eventlistener:theeventlistenername];command=/bin/eventlistener ; the program (relative uses PATH, can take args);process_name=%(program_name)s ; process_name expr (default %(program_name)s);numprocs=1 ; number of processes copies to start (def 1);events=EVENT ; event notif. types to subscribe to (req'd);buffer_size=10 ; event buffer queue size (default 10);directory=/tmp ; directory to cwd to before exec (def no cwd);umask=022 ; umask for process (default None);priority=-1 ; the relative start priority (default -1);autostart=true ; start at supervisord start (default: true);startsecs=1 ; # of secs prog must stay up to be running (def. 1);startretries=3 ; max # of serial start failures when starting (default 3);autorestart=unexpected ; autorestart if exited after running (def: unexpected);exitcodes=0 ; 'expected' exit codes used with autorestart (default 0);stopsignal=QUIT ; signal used to kill process (default TERM);stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10);stopasgroup=false ; send stop signal to the UNIX process group (default false);killasgroup=false ; SIGKILL the UNIX process group (def false);user=chrism ; setuid to this UNIX account to run the program;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB);stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10);stdout_events_enabled=false ; emit events on stdout writes (default false);stdout_syslog=false ; send stdout to syslog with process name (default false);stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB);stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10);stderr_events_enabled=false ; emit events on stderr writes (default false);stderr_syslog=false ; send stderr to syslog with process name (default false);environment=A="1",B="2" ; process environment additions;serverurl=AUTO ; override serverurl computation (childutils); The sample group section below shows all possible group values. Create one; or more 'real' group: sections to create "heterogeneous" process groups.;[group:thegroupname];programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions;priority=999 ; the relative start priority (default 999); The [include] section can just contain the "files" setting. This; setting can list multiple files (separated by whitespace or; newlines). It can also contain wildcards. The filenames are; interpreted as relative to this file. Included files *cannot*; include files themselves.;[include];files = relative/directory/*.ini生成的配置文件模板由多段配置组成,使用英文分号“;”标识注释。
[unix_http_server]
这个配置段,配置http服务监听的 socket,它的作用是为了让supervisorctl 能连接supervisord http server获取进程状态信息,包括管理子进程。
[unix_http_server]file=/tmp/supervisor.sock ; the path to the socket file;chmod=0700 ; socket file mode (default 0700);chown=nobody:nogroup ; socket file uid:gid owner;username=user ; default is no username (open server);password=123 ; default is no password (open server)
[inet_http_server]
Supervisor提供了一个WebUI的管理界面,如果需要打开的话,就在这个配置段设置,port指定监听ip和端口,username 和password指定访问WebUI需要的认证信息。
[supervisord]
Supervisor服务的配置项。
[supervisord]logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.loglogfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MBlogfile_backups=10 ; # of main logfile backups; 0 means none, default 10loglevel=info ; log level; default info; others: debug,warn,tracepidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pidnodaemon=false ; start in foreground if true; default falsesilent=false ; no logs to stdout if true; default falseminfds=1024 ; min. avail startup file descriptors; default 1024minprocs=200 ; min. avail process descriptors;default 200;umask=022 ; process file creation umask; default 022;user=supervisord ; setuid to this UNIX account at startup; recommended if root;identifier=supervisor ; supervisord identifier, default is 'supervisor';directory=/tmp ; default is not to cd during start;nocleanup=true ; don't clean up tempfiles at start; default false;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP;environment=KEY="value" ; key value pairs to add to environment;strip_ansi=false ; strip ansi escape codes in logs; def. false
[supervisorctl]
Supervisor的客户端程序supervisorctl的相关配置,注意serverurl对应的sock就是[unix_http_server]中file的配置项,如果上面配置了用户名密码,此处也需要配置。
[supervisorctl]serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket;username=chris ; should be same as in [*_http_server] if set;password=123 ; should be same as in [*_http_server] if set;prompt=mysupervisor ; cmd line prompt (default "supervisor");history_file=~/.sc_history ; use readline history if available
[include]
指定包含的配置文件的路径,通常被监控的程序配置文件,一个程序一个,更加清晰方便管理。可以将示例中的.ini后缀改为.conf。
;[include];files = relative/directory/*.ini
[program:theprogramname]
具体需要监控的程序配置项,program冒号后面的内容就是程序名称。
;[program:theprogramname];command=/bin/cat ; the program (relative uses PATH, can take args);process_name=%(program_name)s ; process_name expr (default %(program_name)s);numprocs=1 ; number of processes copies to start (def 1);directory=/tmp ; directory to cwd to before exec (def no cwd);umask=022 ; umask for process (default None);priority=999 ; the relative start priority (default 999);autostart=true ; start at supervisord start (default: true);startsecs=1 ; # of secs prog must stay up to be running (def. 1);startretries=3 ; max # of serial start failures when starting (default 3);autorestart=unexpected ; when to restart if exited after running (def: unexpected);exitcodes=0 ; 'expected' exit codes used with autorestart (default 0);stopsignal=QUIT ; signal used to kill process (default TERM);stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10);stopasgroup=false ; send stop signal to the UNIX process group (default false);killasgroup=false ; SIGKILL the UNIX process group (def false);user=chrism ; setuid to this UNIX account to run the program;redirect_stderr=true ; redirect proc stderr to stdout (default false);stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB);stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10);stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0);stdout_events_enabled=false ; emit events on stdout writes (default false);stdout_syslog=false ; send stdout to syslog with process name (default false);stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB);stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10);stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0);stderr_events_enabled=false ; emit events on stderr writes (default false);stderr_syslog=false ; send stderr to syslog with process name (default false);environment=A="1",B="2" ; process environment additions (def no adds);serverurl=AUTO ; override serverurl computation (childutils)
准备springboot程序
准备两个Springboot程序demo01.jar、demo02.jar用于测试。程序内容没什么好说的,主要是提供一个接口来确认程序是否正常运行,并将两个jar包上传至/usr/app目录下。。
package com.zzp;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class Demo01Application { @GetMapping("/hello") public String hello(){ return "hello,demo01"; } public static void main(String[] args) { SpringApplication.run(Demo01Application.class, args); }}package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class Demo02Application { @GetMapping("/hello") public String hello(){ return "hello demo02"; } public static void main(String[] args) { SpringApplication.run(Demo02Application.class, args); }}Springboot Supervisor程序配置
在/etc/supervisor目录下准备两个Springboot对应的Supervisor程序配置文件demo01.conf、demo02.conf,具体的配置文件含义可以参考注释。
[program:demo01]directory = /usr/app ; 程序的启动目录command = java -jar /usr/app/demo01.jar ; 启动命令,可以看出与手动在命令行启动的命令是一样的autostart = true ; 在 supervisord 启动的时候也自动启动startsecs = 30 ; 启动 30 秒后没有异常退出,就当作已经正常启动了autorestart = true ; 程序异常退出后自动重启startretries = 3 ; 启动失败自动重试次数,默认是 3redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 falsestdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MBstdout_logfile_backups = 20 ; stdout 日志文件备份数; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)stdout_logfile = /usr/app/demo01.log ;应用日志目录
[program:demo02]directory = /usr/app ; 程序的启动目录command = java -jar /usr/app/demo02.jar ; 启动命令,可以看出与手动在命令行启动的命令是一样的autostart = true ; 在 supervisord 启动的时候也自动启动startsecs = 30 ; 启动 30 秒后没有异常退出,就当作已经正常启动了autorestart = true ; 程序异常退出后自动重启startretries = 3 ; 启动失败自动重试次数,默认是 3redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 falsestdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MBstdout_logfile_backups = 20 ; stdout 日志文件备份数; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)stdout_logfile = /usr/app/demo02.log ;应用日志目录
修改Supervisor配置文件
修改/etc/supervisord.conf文件中的[include]这段配置,将注释放开并把上面两个程序配置文件加入进去。
[include]files = /etc/supervisor/*.conf
启动supervisord
通过“-c”指定配置文件启动supervisord,否则会默认到当前目录下去寻找。
supervisord -c /etc/supervisord.conf
验证程序
supervisord服务启动成功后,我们就可以通过supervisorctl命令进行交互了。
# 查看管理进程状态supervisorctl status# 停止指定进程,如果是all则是操作全部管理的进程supervisorctl stop [进程名称]# 启动指定进程,如果是all则是操作全部管理的进程supervisorctl start [进程名称]# 重启指定进程,如果是all则是操作全部管理的进程supervisorctl restart [进程名称]# 配置文件修改后使用该命令加载新的配置supervisorctl update# 重新启动配置中的所有程序supervisorctl reload
输入supervisorctl status如果看到以下内容则表示两个springboot服务启动成功。
[root@supervisor etc]# supervisorctl statusdemo01 RUNNING pid 3853, uptime 0:01:50demo02 RUNNING pid 3852, uptime 0:01:50
同样通过ps -ef也能看到对应进程信息。
[root@supervisor etc]# ps -ef| grep javaroot 3852 3851 0 16:36 ? 00:00:07 java -jar /usr/app/demo02.jarroot 3912 3851 1 16:40 ? 00:00:07 java -jar /usr/app/demo01.jarroot 3945 1643 0 16:49 pts/0 00:00:00 grep --color=auto java
访问程序中的接口验证程序正常启动。
[root@supervisor ~]# curl http://127.0.0.1:8001/hellohello,demo01[root@supervisor ~]# curl http://127.0.0.1:8002/hellohello,demo02
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。
您可能感兴趣的文章:- Linux下SpringBoot服务启动配置方法及JVM参数设置教程
- Linux通过vim修改Jar包并重新发布过程(Springboot项目)
- Springboot设置windows和Linux临时上传路径方式
- 部署SpringBoot项目到Linux服务器数据盘的完整流程
- Springboot实现文件上传至linux指定目录
- linux部署springBoot项目的脚本问题
- SpringBoot使用JSch操作Linux的方法
相关文章
- 纸箱尺寸计算公式:详解 07-28
- zk助手如何设置悬浮显示 zk助手设置悬浮显示方法 07-28
- 纸箱尺寸计算公式解读 07-28
- 纸箱的十种创意妙用 07-28
- 纸箱妙用手册 07-28
- 宝燕儿童乐园 游玩指南 07-28