一篇带你了解CentOS 安装Supervisor

系统 Linux
supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具。可以很方便的监听、启动、停止、重启一个或多个进程。

简介

CentOS有自带的supervisor,可以通过yum直接安装。

安装

在CentOS7上可以直接安装,在CentOS Stream上可以通过epel-release安装。

yum -y install supervisor 
  • 1.

服务

CentOS7以上的版本都是使用systemd进行服务管理

systemctl enable supervisord 
systemctl start supervisord 
  • 1.
  • 2.

配置

默认配置文件是/etc/supervisord.conf

[unix_http_server] 
file=/run/supervisor/supervisor.sock   ; (the path to the socket file) 
 
[supervisord] 
logfile=/var/log/supervisor/supervisord.log  ; (main log file;default $CWD/supervisord.log) 
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB) 
logfile_backups=10          ; (num of main logfile rotation backups;default 10) 
loglevel=info               ; (log level;default info; others: debug,warn,trace) 
pidfile=/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) 
nodaemon=false              ; (start in foreground if true;default false
minfds=1024                 ; (min. avail startup file descriptors;default 1024) 
minprocs=200                ; (min. avail process descriptors;default 200) 
 
[rpcinterface:supervisor] 
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface 
 
[supervisorctl] 
serverurl=unix:///run/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket 
 
[include] 
files = supervisord.d/*.ini 
[root@centos8 ~]# cat /etc/supervisord.conf  | grep -v '^;'  | grep -v '^$' 
[unix_http_server] 
file=/run/supervisor/supervisor.sock   ; (the path to the socket file) 
[supervisord] 
logfile=/var/log/supervisor/supervisord.log  ; (main log file;default $CWD/supervisord.log) 
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB) 
logfile_backups=10          ; (num of main logfile rotation backups;default 10) 
loglevel=info               ; (log level;default info; others: debug,warn,trace) 
pidfile=/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) 
nodaemon=false              ; (start in foreground if true;default false
minfds=1024                 ; (min. avail startup file descriptors;default 1024) 
minprocs=200                ; (min. avail process descriptors;default 200) 
[rpcinterface:supervisor] 
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface 
[supervisorctl] 
serverurl=unix:///run/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket 
[include] 
files = supervisord.d/*.ini 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

可以直接修改这个配置直接加promgram管理程序

也可以放到/etc/supervisord.d/*.ini这个目录里面,独立文件方便拷贝管理。

vim /etc/supervisord.d/demo.ini 
  • 1.
[program:demo] 
command=/usr/local/demo/demo              ; 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=/usr/local/demo               ; 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 (defaulttrue
;autorestart=true              ; retstart at unexpected quit (defaulttrue
;startsecs=10                  ; number of secs prog must stay running (def. 1) 
;startretries=3                ; max # of serial start failures (default 3) 
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2) 
;stopsignal=QUIT               ; signal used to kill process (default TERM) 
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10) 
;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 (default 10) 
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0) 
;stdout_events_enabled=false   ; emit events on stdout writes (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 (default 10) 
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0) 
;stderr_events_enabled=false   ; emit events on stderr writes (default false
;environment=A=1,B=2           ; process environment additions (def no adds) 
;serverurl=AUTO                ; override serverurl computation (childutils) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

配置好程序路径就可以加载到任务了。

任务管理

可以通过supervisorctl进行任务控制

supervisorctl status   #查看进程运行状态 
supervisorctl start 进程名 #启动进程 
supervisorctl stop 进程名 #关闭进程 
supervisorctl restart 进程名 #重启进程 
supervisorctl shutdown #关闭supervisord 
supervisorctl reread  #重新载入配置文件 
supervisorctl reload  #重新载入配置并重启 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

句柄不够

在一些高并发的程序可能出现句柄不够用的情况,可以通过编辑supervisord.service文件修改句柄数。

vim /usr/lib/systemd/system/supervisord.service 
  • 1.
[Unit] 
Description=Process Monitoring and Control Daemon 
After=rc-local.service 
 
[Service] 
LimitNOFILE=40960 
LimitNPROC=40960 
Type=forking 
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf 
RuntimeDirectory=supervisor 
RuntimeDirectoryMode=755 
 
[Install] 
WantedBy=multi-user.target 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

 修改之后需要重启服务生效

systemctl daemon-reload 
systemctl restart supervisord 
  • 1.
  • 2.

总结

使用supervisor可以让我们快速管理服务,特别是部署Go程序的时候很方便。

 

责任编辑:姜华 来源: 今日头条
相关推荐

2021-05-20 06:57:16

RabbitMQ开源消息

2021-07-28 10:02:54

建造者模式代码

2023-05-12 08:19:12

Netty程序框架

2021-07-14 08:24:23

TCPIP 通信协议

2021-06-30 00:20:12

Hangfire.NET平台

2021-08-11 07:02:21

npm包管理器工具

2021-11-24 08:51:32

Node.js监听函数

2021-12-15 11:52:34

GPLLinuxGNU

2021-08-02 06:34:55

Redis删除策略开源

2020-11-10 10:48:10

JavaScript属性对象

2021-08-14 10:01:43

Python条件语句Python基础

2021-08-26 05:27:08

Base64 字节流算法

2021-07-08 06:30:03

Linux CPULinux 系统

2021-01-29 18:41:16

JavaScript函数语法

2022-02-23 09:36:11

GoRuby编程语言

2021-02-02 18:39:05

JavaScript

2022-11-10 16:55:41

ReactFiber

2022-02-17 08:35:59

OLTPOLAP数据仓库

2021-06-04 09:56:01

JavaScript 前端switch

2024-04-19 14:23:52

SwitchJavaScript开发
点赞
收藏

51CTO技术栈公众号