chromiumer.com

进程管理工具

2023.02.21

1.supervisor是什么

supervisor是个轻易级并且功能强大的Unix Like 系统进程管理工具。 通过简单配置即可完成服务的启停、重启、异常拉起、日志管理、进程监控等操作。

官网:http://supervisord.org

2.安装配置

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
pip install supervisor
全局配置文件
cat <<'EOF'>> /etc/supervisord.conf
[supervisord]
logfile = /var/log/supervisord.log
logfile_maxbytes = 500MB
logfile_backups = 10
loglevel = info
pidfile = /var/run/supervisord.pid
nodaemon = false
minfds = 1024
minprocs = 200
umask = 022
user = root
identifier = supervisor
directory = /data/supervisord.d
nocleanup = true
childlogdir = /var/log
strip_ansi = false

[supervisorctl]
serverurl = http://127.0.0.1:9001
username =
password =
prompt = supervisor

[inet_http_server]
port = 127.0.0.1:9001
username =
password =

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[include]
files = /data/supervisord.d/*.conf
EOF

3.使用方法

服务配置文件示例
cat <<'EOF'>> /data/supervisord.d/conf.example
[program:prometheus-node-exporter] //服务名称
command         = /data/prometheus/prometheus-node-exporter/node_exporter --web.listen-address="0.0.0.0:9100" //启动命令(前台运行)
directory       = /data/prometheus/prometheus-node-exporter //服务目录
stdout_logfile  = /data/prometheus/prometheus-node-exporter/stdout.log //标准输出日志
user            = root //用户
stopwaitsecs    = 10
stopasgroup     = true
killasgroup     = true
autostart       = true
autorestart     = true
redirect_stderr = true
EOF
启动守护进程
supervisord -c /etc/supervisor.conf
新增服务
[root@VM-4-13-centos ~]# supervisorctl update
prometheus-node-exporter: added process group
启动服务
[root@VM-4-13-centos posts]# supervisorctl start prometheus-node-exporter
prometheus-node-exporter: started
停止服务
[root@VM-4-13-centos posts]# supervisorctl stop prometheus-node-exporter
prometheus-node-exporter: stopped
重启服务
[root@VM-4-13-centos posts]# supervisorctl restart prometheus-node-exporter
prometheus-node-exporter: stopped
prometheus-node-exporter: started
查看服务
[root@VM-4-13-centos ~]# supervisorctl status prometheus-node-exporter
prometheus-node-exporter         RUNNING   pid 1040, uptime 217 days, 12:25:48

说明:通过supervisor管理进程,需要服务以“前台”方式运行,服务进程为supervisor fork出来的子进程。