supervisor进程守护

 

Supervisor是一个客户端/服务器系统。我是用他是为了让Asp.Net Core程序可以在ubuntu服务器上一直运行下去;

我们开发好程序之后;在ubuntu 使用dotnet命令可以运行写好的程序;但是关闭xshell工具,这个进程也就关闭了;这时就用到了Supervisor,这篇博客主要是巩固自己学的东西,以及所遇到的一些错误以及解决方案;

 

Supervisor安装

#安装supervisor

sudo apt-get install supervisor

#常用命令

#重启
service supervisor restart

#启动
service supervisor start 

#停止  
service supervisor stop

#查看进程
netstat -ntpl

#杀死进程
kill 进程ID

创建文件夹
mkdir 文件名

删除具体文件
rm -f 文件名

删除该文件夹下所有文件
rm -rf *

创建文件
touch 文件名.文件类型

查看当前目录
ls

编辑文件
vim 文件名

编辑文件进入之后的一些命令(这些命令需要按Esc再执行)
取消前一个操作
u

保存并退出
wq

不保存并退出
q!

 

配置Supervisor

安装完成之后会有一些目录:

conf.d 文件夹下可以存放我们写的配置文件,去运行你需要执行的进程

具体配置为

[program:TestApiInfo]
command=dotnet Hubert.Api.Demo.dll    #需要运行的dll
directory=/home/hubert/dotnetcore/testapi   #你的程序路径(注意权限问题)
autostart=true         #是否自动启动
autorestart=true      #是否自动重启
stderr_logfile=/var/log/TestApiInfo.err.log     #错误日志
stdout_logfile=/var/log/TestApiInfo.out.log    #输出日志
environment=Hosting__Environment=Production
user=root               #进程执行的用户身份
stopsignal=INT
redirect_stderr=true

 

 

supervisord.conf 是supervisor配置文件 

[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

 

遇到的问题

supervisor: couldn't chdir to /public/dotnetcore/demoapiproject/: ENOENT
supervisor: child process was not spawned

这是因为原先存放程序的路径问题,supervisor无法读取到该程序,所以切换到home路径,未出现这个问题;(自己这么解决的...)

 

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

运行这个命令解决了

sudo unlink /run/supervisor.sock

 

最后测试你的程序是否被supervisor守护

1、查看所有子进程的状态

root@www:/etc/supervisor/conf.d# supervisorctl status
TestApiInfo                      RUNNING   pid 12694, uptime 0:45:18
apidemo                          FATAL     Exited too quickly (process log may have details)
root@www:/etc/supervisor/conf.d# 

 

2、查看你需要监控守护的进程,并且杀死该进程再查看该进程是否还在

root@www:~# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      23636/nginx: master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      23636/nginx: master 
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      363/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      775/sshd            
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      7128/sshd: root@pts 
tcp6       0      0 :::55555                :::*                    LISTEN      745/v2ray           
tcp6       0      0 :::53128                :::*                    LISTEN      745/v2ray           
tcp6       0      0 :::5006                 :::*                    LISTEN      12694/dotnet        
tcp6       0      0 :::80                   :::*                    LISTEN      23636/nginx: master 
tcp6       0      0 :::8080                 :::*                    LISTEN      17184/docker-proxy  
tcp6       0      0 :::22                   :::*                    LISTEN      775/sshd            
tcp6       0      0 ::1:6010                :::*                    LISTEN      7128/sshd: root@pts 
root@www:~# kill 12694
root@www:~# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      23636/nginx: master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      23636/nginx: master 
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      363/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      775/sshd            
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      7128/sshd: root@pts 
tcp6       0      0 :::55555                :::*                    LISTEN      745/v2ray           
tcp6       0      0 :::53128                :::*                    LISTEN      745/v2ray           
tcp6       0      0 :::5006                 :::*                    LISTEN      15984/dotnet        
tcp6       0      0 :::80                   :::*                    LISTEN      23636/nginx: master 
tcp6       0      0 :::8080                 :::*                    LISTEN      17184/docker-proxy  
tcp6       0      0 :::22                   :::*                    LISTEN      775/sshd            
tcp6       0      0 ::1:6010                :::*                    LISTEN      7128/sshd: root@pts 
root@www:~# 

 

posted @ 2018-10-28 22:12  Hubert_Biyo  阅读(4142)  评论(0编辑  收藏  举报