supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中就好了。

此时被管理进程被视为supervisor的子进程,若该子进程异常中断,则父进程可以准确的获取子进程异常中断的信息,通过在配置文件中设置autostart=ture,可以实现对异常中断的子进程的自动重启。

安装

yum install supervisor
touch /var/run/supervisor/supervisor.sock
chmod 777 /var/run/supervisor/supervisor.sock
supervisord -c /etc/supervisord.conf supervisord
supervisorctl start all
supervisorctl status

项目配置目录:/etv/supervisor.d

基于swoft的配置

[program:user-center-gift]
process_name=%(program_name)s_%(process_num)02d
command=/usr/local/php/bin/php /home/www/userCenter/bin/swoft gift:write
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/home/www/supervisor_user-center-gift.log

命令

#查看所有服务的状态
supervisorctl status

服务名 = user-center-gift:user-center-gift_00
#启动服务
supervisorctl start 服务名
#停止服务
supervisorctl stop 服务名
#重启服务
supervisorctl restart 服务名

常见问题

端口占用

Starting supervisor: 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.
For help, use /usr/bin/supervisord -h

显示的是有另一个进程占用了监听端口

查出来 kill掉
ps -ef | grep supervisord

提示setuptools版本问题

/usr/local/lib/python2.7/dist-packages/pkg_resources/py2_warn.py:21: UserWarning: Setuptools will stop working on Python 2
************************************************************
You are running Setuptools on Python 2, which is no longer
supported and
>>> SETUPTOOLS WILL STOP WORKING <<<
in a subsequent release (no sooner than 2020-04-20).
Please ensure you are installing
Setuptools using pip 9.x or later or pin to `setuptools<45`
in your environment.
If you have done those things and are still encountering
this message, please follow up at
https://bit.ly/setuptools-py2-warning.
************************************************************
sys.version_info < (3,) and warnings.warn(pre + "*" * 60 + msg + "*" * 60)
supervisor>

先看下版本

root@xxxxxx:~# which python
/usr/bin/python
root@xxxxxx:~# /usr/bin/python -V
Python 2.7.12 root@xxxxxx:~# which pip
/usr/bin/pip
root@xxxxxx:~# pip -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

升级

pip2 install --upgrade --user pip

#使用pip 9.x或更高版本的Setuptools或固定setuptools<45在您的环境中
pip2 install --user "setuptools<45"

supervisor 详细配置

#项目名
[program:blog]
#脚本目录
directory=/opt/bin
#脚本执行命令
command=/usr/bin/python /opt/bin/test.py
#supervisor启动的时候是否随着同时启动,默认True
autostart=true
#当程序exit的时候,这个program不会自动重启,默认unexpected
#设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。如果为false的时候,无论什么情况下,都不会被重新启动,如果为unexpected,只有当进程的退出码不在下面的exitcodes里面定义的
autorestart=false
#进程从STARING状态转换到RUNNING状态program所需要保持运行的时间(单位:秒)
startsecs=1
#日志输出
stderr_logfile=/tmp/blog_stderr.log
stdout_logfile=/tmp/blog_stdout.log
#脚本运行的用户身份
user = zhoujy
#把 stderr 重定向到 stdout,默认 false
redirect_stderr = true
#stdout 日志文件大小,默认 50MB
stdout_logfile_maxbytes = 20M
#stdout 日志文件备份数
stdout_logfile_backups = 20
[program:zhoujy] #说明同上
directory=/opt/bin
command=/usr/bin/python /opt/bin/zhoujy.py
autostart=true
autorestart=false
stderr_logfile=/tmp/zhoujy_stderr.log
stdout_logfile=/tmp/zhoujy_stdout.log
#user = zhoujy
子进程配置详细说明
;[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,2 ; 'expected' exit codes used with autorestart (default 0,2)
;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 (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)
;[program:theprogramname] ;这个就是咱们要管理的子进程了,":"后面的是名字,最好别乱写和实际进程有点关联最好。这样的program我们可以设置一个或多个,一个program就是要被管理的一个进程
;command=/bin/cat ; 这个就是我们的要启动进程的命令路径了,可以带参数 例子:/home/test.py -a 'hehe'有一点需要注意的是,我们的command只能是那种在终端运行的进程,不能是守护进程。
这个想想也知道了,比如说command=service httpd start。httpd这个进程被linux的service管理了,我们的supervisor再去启动这个命令这已经不是严格意义的子进程了。这个是个必须设置的项
;process_name=%(program_name)s ; 这个是进程名,如果我们下面的numprocs参数为1的话,就不用管这个参数了,它默认值%(program_name)s也就是上面的那个program冒号后面的名字,但是如果numprocs为多个的话,那就不能这么干了。想想也知道,不可能每个进程都用同一个进程名吧。
;numprocs=1 ; 启动进程的数目。当不为1时,就是进程池的概念,注意process_name的设置默认为1 。。非必须设置
;directory=/tmp ; 进程运行前,会前切换到这个目录默认不设置。。。非必须设置
;umask=022 ; 进程掩码,默认none,非必须
;priority=999 ; 子进程启动关闭优先级,优先级低的,最先启动,关闭的时候最后关闭默认值为999 。。非必须设置
;autostart=true ; 如果是true的话,子进程将在supervisord启动后被自动启动默认就是true 。。非必须设置
;autorestart=unexpected ; 这个是设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。如果为false的时候,无论什么情况下,都不会被重新启动,
如果为unexpected,只有当进程的退出码不在下面的exitcodes里面定义的退出码的时候,才会被自动重启。当为true的时候,只要子进程挂掉,将会被无条件的重启
;startsecs=1 ; 这个选项是子进程启动多少秒之后,此时状态如果是running,则我们认为启动成功了 默认值为1 。。非必须设置
;startretries=3 ; 当进程启动失败后,最大尝试启动的次数。。当超过3次后,supervisor将把此进程的状态置为FAIL默认值为3 。。非必须设置
;exitcodes=0,2 ; 注意和上面的的autorestart=unexpected对应。。exitcodes里面的定义的退出码是expected的。
;stopsignal=QUIT ; 进程停止信号,可以为TERM, HUP, INT, QUIT, KILL, USR1, or USR2等信号默认为TERM 。。当用设定的信号去干掉进程,退出码会被认为是expected 非必须设置
;stopwaitsecs=10 ; 这个是当我们向子进程发送stopsignal信号后,到系统返回信息给supervisord,所等待的最大时间。 超过这个时间,supervisord会向该子进程发送一个强制kill的信号。默认为10秒。。非必须设置
;stopasgroup=false ; 这个东西主要用于,supervisord管理的子进程,这个子进程本身还有子进程。那么我们如果仅仅干掉supervisord的子进程的话,子进程的子进程有可能会变成孤儿进程。所以咱们可以设置可个选项,把整个该子进程的整个进程组都干掉。
设置为true的话,一般killasgroup也会被设置为true。需要注意的是,该选项发送的是stop信号 默认为false。。非必须设置。。
;killasgroup=false ; 这个和上面的stopasgroup类似,不过发送的是kill信号
;user=chrism ; 如果supervisord是root启动,我们在这里设置这个非root用户,可以用来管理该program默认不设置。。。非必须设置项
;redirect_stderr=true ; 如果为true,则stderr的日志会被写入stdout日志文件中默认为false,非必须设置
;stdout_logfile=/a/path ; 子进程的stdout的日志路径,可以指定路径,AUTO,none等三个选项。设置为none的话,将没有日志产生。设置为AUTO的话,将随机找一个地方生成日志文件,而且当supervisord重新启动的时候,以前的日志文件会被清空。
当 redirect_stderr=true的时候,sterr也会写进这个日志文件
;stdout_logfile_maxbytes=1MB ; 日志文件最大大小,和[supervisord]中定义的一样。默认为50
;stdout_logfile_backups=10 ; 和[supervisord]定义的一样。默认10
;stdout_capture_maxbytes=1MB ; 这个东西是设定capture管道的大小,当值不为0的时候,子进程可以从stdout发送信息,而supervisor可以根据信息,发送相应的event。默认为0,为0的时候表达关闭管道。。。非必须项
;stdout_events_enabled=false ; 当设置为ture的时候,当子进程由stdout向文件描述符中写日志的时候,将触发supervisord发送PROCESS_LOG_STDOUT类型的event默认为false。。。非必须设置
;stderr_logfile=/a/path ; 这个东西是设置stderr写的日志路径,当redirect_stderr=true。这个就不用设置了,设置了也是白搭。因为它会被写入stdout_logfile的同一个文件中默认为AUTO,也就是随便找个地存,supervisord重启被清空。。非必须设置
;stderr_logfile_maxbytes=1MB ; 这个出现好几次了,就不重复了
;stderr_logfile_backups=10 ; 这个也是
;stderr_capture_maxbytes=1MB ; 这个一样,和stdout_capture一样。 默认为0,关闭状态
;stderr_events_enabled=false ; 这个也是一样,默认为false
;environment=A="1",B="2" ; 这个是该子进程的环境变量,和别的子进程是不共享的
;serverurl=AUTO ;

日志目录

如果supervisor遇到错误,可以在/var/log/supervisor/supervisord.log中查看日志;

最新文章

  1. SQL开发技巧(二)
  2. 淘宝ip库接口调用
  3. Array和ArrayList互相转换
  4. IOS 作业项目(4)步步完成 画图 程序(问题处理)终结
  5. C#转换日期类型
  6. Android 应用启动渐变效果
  7. 向未声明的 JavaScript 变量来分配值
  8. 动态面板——axure线框图部件库介绍
  9. jquery 学习笔记 (2)--write less,do more
  10. 全方位分析web前端如何进行性能优化
  11. 从线程池到synchronized关键字详解
  12. idea 编译 内存不足
  13. PostGIS集群
  14. 实现Runnable接口创建多线程及其优势
  15. LOCALDB安装和连接
  16. java 之 dom4j解析xml
  17. HanLP 配置与使用
  18. Oracle NET工作原理、配置及连接问题排查
  19. kkpager的改进,Ajax数据变化但是页码不变的有关问题
  20. Sprint2

热门文章

  1. 手把手教你 Docker搭建nacos单机版
  2. redis linux的 安装
  3. html table 固定列
  4. iMAC_使用相关
  5. 定要过python二级 真题 第四套
  6. 如何一次性add library to classpath
  7. CSharpEntityFramework与CodeFirst实践
  8. 独家对话阿里云函数计算负责人不瞋:你所不知道的 Serverless
  9. ShutdownHook原理
  10. aritest发送测试报告到邮件