Linux配置supervisor管理golang服务环境

Golang
382
0
0
2022-10-28
标签   Golang基础
一、安装服务
使用yum命令安装(推荐)
$ sudo su - #切换为root用户

# yum install epel-release
# yum install -y supervisor
# systemctl enable supervisord # 开机自启动
# systemctl start supervisord # 启动supervisord服务

# systemctl status supervisord # 查看supervisord服务状态
# ps -ef|grep supervisord # 查看是否存在supervisord进程
使用pip手工安装配置(不推荐)
自行百度
二、常用命令
  • 基本命令:
echo_supervisord_conf > /etc/supervisord.conf  # 创建supervisor配置文件

ps -ef|grep supervisord      # 查看是否存在supervisord进程
systemctl enable supervisord # 开机自启动
systemctl start supervisord  # 启动supervisord服务
systemctl status supervisord # 查看supervisord服务状态
supervisorctl reload         # 重新加载配置文件
其他:
systemctl stop supervisord
systemctl start supervisord
systemctl status supervisord
systemctl reload supervisord
systemctl restart supervisord
  • 常用命令:
sudo supervisorctl restart server_name #重启某个服务
sudo supervisorctl start server_name   #启动某个服务
sudo supervisorctl stop server_name    #停止某个服务
sudo supervisorctl status server_name  #查看某个服务运行情况
sudo supervisorctl  #查看所有进程情况
三、基本使用和案例
  1. 准备项目代码
# 此处使用go项目,需要先部署golang环境,详见Linux部署golang环境文档,其他语言项目也需要部署对应环境
vim /usr/local/server-path/go-http/http-server.go
package main

import(
    "fmt" 
    "log" 
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello Supervisor")
    })

    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}
  1. 编译
cd vim /usr/local/server-path/go-http
go build http-server.go
  1. supervisor配置文件更新,使用常用的.conf文件
 vim /etc/supervisord.conf 
 找到末尾,加上:
 [include] 
 files = supervisord.d/*.ini
 files = supervisord.d/*.conf
  1. 在supervisord.d/目录下新建配置test.conf
mkdir /etc/supervisord.d/test.conf
  1. 写入配置,更多参考:blog.csdn.net/antch620/article/det...
[program:服务名称]
directory = 服务二进制文件执行路径
command= 服务二进制文件执行路径/二进制程序名称
autostart=true
autorestart=true
startsecs=10
stdout_logfile_maxbytes=1MB
stdout_logfile_backuos=10
stderr_logfile_maxbytes=1MB
stderr_logfile_backuos=10
stderr_capture_maxbytes=1MB
  1. 例如本例:
[program:go-http-server]
directory = /usr/local/server-path/go-http
command= /usr/local/server-path/go-http/http-server
autostart=true
autorestart=true
startsecs=10
stdout_logfile_maxbytes=1MB
stdout_logfile_backuos=10
stderr_logfile_maxbytes=1MB
stderr_logfile_backuos=10
stderr_capture_maxbytes=1MB
  1. supervisor重加载配置
supervisorctl reload #重新加载配置文件
systemctl restart supervisord
或者:
systemctl start supervisord
  1. 查看进程,可以看到新进程实现了托管
supervisorctl