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:~#
