Supervisor进程管理并设置为开机自动启动

描述

Supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

不使用守护进程会出现的三个问题:

  • 1、ASP.NET Core应用程序运行在shell之中,如果关闭shell则会发现 ASP.NET Core程序被关闭,从而导致应用无法访问,这种情况当然是我们不想遇到的,而且生产环境对这种情况是零容忍的。
  • 2、如果 ASP.NET Core进程意外终止那么需要人为连进shell进行再次启动,往往这种操作都不够及时。
  • 3、如果服务器宕机或需要重启,我们则还是需要连入shell进行启动。

为了解决这些问题,我们需要有一个程序来监听 ASP.NET Core 应用程序的状况。并在应用程序停止运行的时候立即重新启动。

假设我们的电脑系统为Ubuntu16.04,我们的需求是:电脑能一直开启某个进程,进程中断时能自动重启

1. 安装supervisor

安装命令

sudo apt-get install supervisor

2. 熟悉supervisor的配置文件

安装后,会发现在/etc/supervisor/路径下存在这样一个配置文件supervisor.conf
打开这个文件

gedit /etc/supervisor/supervisor.conf

发现supervisor.conf里是这么写的

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below 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: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; 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 = /etc/supervisor/conf.d/*.conf
  • “;”分号后面的是注释
  • 其他的配置语句可以自行学习,这里关注最后一句话
    [include]
    files = /etc/supervisor/conf.d/*.conf
    
    这句话的作用是:我们可以配置自己想要启动的进程,我在下面一节会进行解释

2. 配置想要启动的进程

我们有两种方式来配置

  1. 写成单独的配置文件.conf
  2. 写在supervisor.conf里

第一种:写成单独的配置文件.conf

需要supervisor.conf文件中包含这句话

[include]
files = /etc/supervisor/conf.d/*.conf

比如我们有一个进程detect,它位于某个路径之下,你可以把下面这些话写在 /etc/supervisor/conf.d/路径下的detect.conf文件中,文件.conf前的名字可以自行设定

[program:detect]
command=/home/chen/detect/build/detect   ;进程的启动命令(detect是一个可执行进程)
process_name=%(program_name)s  ;进程名字(这是默认的写法)
numprocs=1 ;启动几个该进程
autostart=true ;在supervisord启动时也自动启动
autorestart=true ;程序异常退出后自动重启
directory=/home/chen/detect/build ;程序的启动目录

第二种:写在supervisor.conf里

还是把下面这句话写一下

[program:detect]
command=/home/chen/detect/build/detect   ;进程的启动命令(detect是一个可执行进程)
process_name=%(program_name)s  ;进程名字(这是默认的写法)
numprocs=1 ;启动几个该进程
autostart=true ;在supervisord启动时也自动启动
autorestart=true ;程序异常退出后自动重启
directory=/home/chen/detect/build ;程序的启动目录

只不过这时,把它填写在 /etc/supervisor/supervisor.conf文件的最后

两种方式都可以实现进程监管

无论是单独写成.conf,还是写到supervisor.conf中,别忘了保存

3. 重新启动supervisor

使用命令来看一下当前电脑上是否已经有supervisor在跑了

ps -A | grep supervisord

如果已经有supervisor在跑,杀掉它

sudo kill [supervisord进程id]

使用如下命令,重新启动supervisor并更新supervisor的配置

sudo supervisor -c /etc/supervisor/supervisor.conf

如果你在使用上述命令时报错,错误内容是

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

意思就是电脑上已经有supervisor在跑了,你按照上述方法杀掉它再重新启动就是了

4. 查看进程是否已经被监管

在supervisor.conf或者*.conf文件中,你为每个进程设置了各自的名字,[program:detect]中program后面接的,就是进程名字

执行命令,来查看detect是否已经启动

ps -A | grep detect

执行sudo kill来杀掉已经启动的detect进程,如果重新执行ps -A | grep detect发现detect进程再次被启动,进程监管成功

5. supervisor自启动

现在所有你需要的进程已经可以被supervisor监管,谁异常退出都会重新启动。但是,如果杀掉supervisor,显然这些进程出现问题后,不会被重新启动

那么一个很重要的问题就来了。supervisor挂掉不属于我们的处理范畴,我们想要的就是supervisor可以在开机时自动启动,监管并启动我们想要的进程

做法也很简单

  1. 执行命令
    sudo vim /etc/rc.local
    
  2. 将下面这句话添加文件rc.localexit 0一行的前面
    /usr/bin/supervisord
    
    保存并退出
    这里需要特别注意,/usr/bin/supervisord路径就是安装在你电脑上supervisord的路径,如果你使用的是apt-get installsupervisord可执行文件就会安装在这里
  3. 最后修改rc.local权限
    sudo chmod +x /etc/rc.local
    
  4. 关机重新启动,会发现supervisord已经随开机自动运行了
    ps -A | grep supervisord
    

这里注意,我们的系统是Ubuntu16.04,看网上有人说Ubuntu18.04已经取消了rc.local文件,18我没有实践过,16这么搞肯定是没问题的

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在CentOS中设置Supervisor开机自启可以按照以下步骤进行操作: 1. 打开Supervisor启动脚本文件: ```shell vim /etc/init.d/supervisord ``` 2. 在脚本文件中添加以下内容: ```shell #!/bin/bash # # supervisord Startup script for the Supervisor process control system # # chkconfig: - 64 36 # description: Supervisor is a client/server system to control \ # all processes on the local machine. # processname: supervisord # Source function library . /etc/rc.d/init.d/functions # Path to the supervisorctl script, server binary, # and short-form for messages. supervisorctl=/usr/local/bin/supervisorctl supervisord=${SUPERVISORD-/usr/local/bin/supervisord} prog=supervisord pidfile=${PIDFILE-/var/run/supervisord.pid} lockfile=${LOCKFILE-/var/lock/subsys/supervisord} STOP_TIMEOUT=${STOP_TIMEOUT-60} OPTIONS="${OPTIONS--c /etc/supervisord.conf}" RETVAL=0 start() { echo -n $"Starting $prog: " daemon --pidfile=${pidfile} $supervisord $OPTIONS RETVAL=$? echo [ $RETVAL -eq 0 ] && touch ${lockfile} return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d ${STOP_TIMEOUT} $supervisord RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -rf ${lockfile} ${pidfile} } reload() { echo -n $"Reloading $prog: " LSB=1 killproc -p $pidfile $supervisord -HUP RETVAL=$? echo } restart() { stop start } case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $supervisord RETVAL=$? [ $RETVAL -eq 0 ] && echo || echo -n " not" ;; restart) restart ;; condrestart|try-restart) if status -p ${pidfile} $supervisord >&/dev/null; then stop start fi ;; force-reload|reload) reload ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload}" RETVAL=2 esac exit $RETVAL ``` 3. 保存并退出文件。 4. 设置脚本文件的执行权限: ```shell chmod +x /etc/init.d/supervisord ``` 5. 启用Supervisor开机自启: ```shell systemctl enable supervisord ``` 6. 验证Supervisor是否已经配置为开机自启: ```shell systemctl is-enabled supervisord ``` 如果输出结果为"enabled",则表示Supervisor已成功配置为开机自启。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值