前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux进程管理工具Supervisor

Linux进程管理工具Supervisor

作者头像
Tinywan
发布2019-07-16 13:01:19
9990
发布2019-07-16 13:01:19
举报
文章被收录于专栏:开源技术小栈

简述

Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。

它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。

也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。

安装

  • 源码地址:https://github.com/Supervisor/supervisor
  • Debian / Ubuntu 可以直接通过apt安装:$ sudo apt-get install supervisor

配置文件

  • 目录结构 1supervisor 2├── conf.d 3│ └── echo_time.conf -- 业务配置文件 4└── supervisord.conf -- 主配置文件,一般不需要改动
  • 使用 apt-get 安装后,supervisor 的主配置文件在/etc/supervisor/supervisord.conf
  • 子进程配置文件在 /etc/supervisor/conf.d/*.conf

supervisord 和 supervisorctl的关系

supervisord 是主进程
  • 常用命令 1# 使用默认的配置文件 /etc/supervisord.conf 2supervisord 3# 明确指定配置文件 4supervisord -c /etc/supervisord.conf 5# 使用 user 用户启动 supervisord 6supervisord -u user
  • 服务命令
    • 查看服务状态:sudo systemctl status supervisor.service
    • 开启服务:sudo systemctl start supervisor.service
    • 停止服务:sudo systemctl stop supervisor.service
    • 重启服务:sudo systemctl restart supervisor.service
    • 开机启动项:sudo systemctl enable supervisor.service
  • supervisor.service 文件 1[Unit] 2Description=Supervisor process control system for UNIX 3Documentation=http://supervisord.org 4After=network.target 5 6[Service] 7ExecStart=/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf 8ExecStop=/usr/bin/supervisorctl 9ExecReload=/usr/bin/supervisorctl -c /etc/supervisor/supervisord.conf <span class="katex-html" aria-hidden="true" style="font-size: inherit;color: inherit;line-height: inherit;"><span class="strut" style="height:1em;vertical-align:-0.25em;" style="font-size: inherit;color: inherit;line-height: inherit;"><span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">O<span class="mord mathit" style="margin-right:0.13889em;" style="font-size: inherit;color: inherit;line-height: inherit;">P<span class="mord mathit" style="margin-right:0.13889em;" style="font-size: inherit;color: inherit;line-height: inherit;">T<span class="mord mathit" style="margin-right:0.07847em;" style="font-size: inherit;color: inherit;line-height: inherit;">I<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">O<span class="mord mathit" style="margin-right:0.10903em;" style="font-size: inherit;color: inherit;line-height: inherit;">N<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">S<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">h<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">t<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">d<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="margin-right:0.02691em;" style="font-size: inherit;color: inherit;line-height: inherit;">w<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">n<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">E<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">x<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">c<span class="mord mathit" style="margin-right:0.00773em;" style="font-size: inherit;color: inherit;line-height: inherit;">R<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="margin-right:0.01968em;" style="font-size: inherit;color: inherit;line-height: inherit;">l<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">a<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">d<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">E<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">E<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">b<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">i<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">n<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">p<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;">v<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">i<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">c<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">t<span class="mord mathit" style="margin-right:0.01968em;" style="font-size: inherit;color: inherit;line-height: inherit;">l<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">E<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;">E<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">c<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">t<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">c<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">p<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;">v<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">i<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">p<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;">v<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">i<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">d<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">c<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">n<span class="mord mathit" style="margin-right:0.10764em;" style="font-size: inherit;color: inherit;line-height: inherit;">fOPTIONS reload 10KillMode=process 11Restart=on-failure 12RestartSec=50s 13 14[Install] 15WantedBy=multi-user.target 16</span class="mord mathit" style="margin-right:0.10764em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.01968em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.01968em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.00773em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02691em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.10903em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit" style="margin-right:0.07847em;"></span class="mord mathit" style="margin-right:0.13889em;"></span class="mord mathit" style="margin-right:0.13889em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="strut" style="height:1em;vertical-align:-0.25em;"></span class="katex-html" aria-hidden="true">
supervisorctl 是客户端程序

用于向supervisord服务发起命令。

  • 所有命令,通过 supervisorctl -h 可以查看帮助说明 1sudo supervisorctl help 2 3default commands (type help <topic>): 4===================================== 5add exit open reload restart start tail 6avail fg pid remove shutdown status update 7clear maintail quit reread signal stop version
  • 控制子进程命令 1# 停止某一个进程,program_name 为 [program:x] 里的 x 2supervisorctl stop program_name 3# 启动某个进程 4supervisorctl start program_name 5# 重启某个进程 6supervisorctl restart program_name 7# 结束所有属于名为 groupworker 这个分组的进程 (start,restart 同理) 8supervisorctl stop groupworker: 9# 结束 groupworker:name1 这个进程 (start,restart 同理) 10supervisorctl stop groupworker:name1 11# 停止全部进程,注:start、restart、stop 都不会载入最新的配置文件 12supervisorctl stop all 13# 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程 14supervisorctl reload 15# 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启 16supervisorctl update
  • 修改进程配置文件后,只要 sudo systemctl restart supervisor.service则会自动加载哦

示例

  • 以简单的/home/www/python/echo_time.sh 为例 1#/bin/bash 2 3while true; 4do 5 echo date +%Y-%m-%d,%H:%m:%s 6 sleep 2 7done
  • /etc/supervisor/conf.d/新增子进程配置文件echo_time.conf 1; /etc/supervisor/conf.d/echo_time.conf 2 3[program:echo_time] 4 5command = /usr/bin/env sh /home/www/python/echo_time.sh 6directory = /home/www/python 7user = www 8startsecs = 3 9 10redirect_stderr = true 11stdout_logfile_maxbytes = 50MB 12stdout_logfile_backups = 10 13stdout_logfile = /home/www/python/log/echo_time.log
  • 然后启动程序: 1 2<span class="katex-html" aria-hidden="true" style="font-size: inherit;color: inherit;line-height: inherit;"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;" style="font-size: inherit;color: inherit;line-height: inherit;"><span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">p<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;">v<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">i<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">s<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">o<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">c<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">t<span class="mord mathit" style="margin-right:0.01968em;" style="font-size: inherit;color: inherit;line-height: inherit;">l<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">e<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">a<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;">d supervisorctl update 3</span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit" style="margin-right:0.01968em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span class="katex-html" aria-hidden="true"> 这两个命令分别代表重新读取配置、更新子进程组
  • 这样刚才添加的 echo_time脚本就常驻运行起来了。可以通过日志查看运行情况: 1tail -f log/echo_time.log 22018-12-22,22:12:1545490695 32018-12-22,22:12:1545490697 42018-12-22,22:12:1545490699 52018-12-22,22:12:1545490701
  • 也可以使用 sudo supervisorctl status查看子进程运行情况: 1sudo supervisorctl status 2echo_time RUNNING pid 28906, uptime 0:08:36

web界面操作

  • 需要开启主配置文件 supervisord.conf注释掉的这4行 (/etc/supervisor/supervisord.conf) 1[inet_http_server] ; inet (TCP) server disabled by default 2port=*:9001 ; (ip_address:port specifier, *:port for ;all iface) 3;username=www ; (default is no username (open server)) 4;password=www123456 ; (default is no password (open server)) 注意:如果修改配置文件时, [inet_http_server]这一行被注释,会导致不仅web需要认证,命令行使用 supervisorctl也需要认证,这时候就需要在交互式命令行里输入用户名、密码才能进行下一步的操作。
  • 浏览器访问:http://myip:9001 ,输入用户名、密码后,即可看到web页面:

遇到的错误

  • Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord

监控工具

  • 一个很简单的PHP监控工具

参考

  • unix:///var/run/supervisor.sock no such file on Ubuntu 16.04
  • How To Use Systemctl to Manage Systemd Services and Units
  • Supervisor使用教程
  • python supervisor使用
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Tinywan的杂货摊 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简述
  • 安装
  • 配置文件
  • supervisord 和 supervisorctl的关系
    • supervisord 是主进程
      • supervisorctl 是客户端程序
      • 示例
      • web界面操作
      • 遇到的错误
      • 监控工具
      • 参考
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档