栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux+golang配置supervisor管理go服务进程

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux+golang配置supervisor管理go服务进程

Linux配置supervisor管理服务环境 一、安装服务
  1. 使用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进程
    
  2. 使用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(":8082", nil)
        if err != nil {
            log.Fatal("ListenAndServe: ", err)
        }
    }
    
  2. 编译

    cd vim /usr/local/server-path/go-http
    go build http-server.go
    
  3. supervisor配置文件更新,使用常用的.conf文件

     vim /etc/supervisord.conf 
     找到末尾,加上:
     [include] 
     files = supervisord.d/*.ini
     files = supervisord.d/*.conf
    
  4. 在supervisord.d/目录下新建配置test.conf

    mkdir /etc/supervisord.d/test.conf
    
  5. 写入配置

    [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
    

例如本例:

[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
    
  2. 查看进程,可以看到新进程实现了托管

    supervisorctl
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962450.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号