描述
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. 配置想要启动的进程
我们有两种方式来配置
- 写成单独的配置文件.conf
- 写在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可以在开机时自动启动,监管并启动我们想要的进程
做法也很简单
- 执行命令
sudo vim /etc/rc.local
- 将下面这句话添加文件
rc.local
中exit 0
一行的前面
保存并退出/usr/bin/supervisord
这里需要特别注意,/usr/bin/supervisord
路径就是安装在你电脑上supervisord的路径,如果你使用的是apt-get install
,supervisord
可执行文件就会安装在这里 - 最后修改rc.local权限
sudo chmod +x /etc/rc.local
- 关机重新启动,会发现supervisord已经随开机自动运行了
ps -A | grep supervisord
这里注意,我们的系统是Ubuntu16.04,看网上有人说Ubuntu18.04已经取消了rc.local文件,18我没有实践过,16这么搞肯定是没问题的