• Linux


    Supervisor是采用 Python(2.4+) 开发的,它是一个允许用户管理 基于 Unix 系统进程的 Client/Server 系统,提供了大量功能来实现对进程的管理。

    目前存在三个问题

    问题1:ASP.NET Core应用程序运行在shell之中,如果关闭shell则会发现ASP.NET Core应用被关闭,从而导致应用无法访问,这种情况当然是我们不想遇到的,而且生产环境对这种情况是零容忍的。 

    问题2:如果ASP.NET Core进程意外终止那么需要人为连进shell进行再次启动,往往这种操作都不够及时。 

    问题3:如果服务器宕机或需要重启我们则还是需要连入shell进行启动。

    为了解决这个问题,我们需要有一个程序来监听ASP.NET Core 应用程序的状况。在应用程序停止运行的时候立即重新启动。

    root权限操作如下步骤:

    1、  安装Supervisor

    执行以下命令:

    cd /etc
    yum install python-setuptools easy_install supervisor

    或者

    如果easy_install不好使就从官方下载:
    wget https://pypi.python.org/packages/80/37/964c0d53cbd328796b1aeb7abea4c0f7b0e8c7197ea9b0b9967b7d004def/supervisor-3.3.1.tar.gz
    然后通过python安装:
    tar zxf supervisor-3.3.1.tar.gz
    cd supervisor
    python setup.py install

    如果报错可能:

    1. 提示setuptools-0.6c11.tar没有安装
      当前目录下载wget https://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
      tar zxf setuptools-0.6c11.tar.gz
      cd setuptools-0.6c11/
      python setup.py build
      python setup.py  install(执行目录:/etc/supervisor-3.3.1/setuptools-0.6c11)
    2. 提示下载错误,需meld3>0.6.5
      1. 下载wget  http://dl.fedoraproject.org/pub/epel/7/x86_64/p/python-meld3-0.6.10-1.el7.x86_64.rpm
      2. 安装 rpm -ivh python-meld3-0.6.10-1.el7.x86_64.rpm

      出现如下提示,安装完成:

    1
    2
    Using /usr/lib64/python2.7/site-packages
    Finished processing dependencies for supervisor==3.3.1

    2、 配置Supervisor

    a.配置文件

    mkdir /etc/supervisor
    echo_supervisord_conf >/etc/supervisor/supervisord.conf #箭头后面不要有空格哦!

    b.修改/etc/supervisor/supervisord.conf文件内容

    在文件结尾[include]节点处

    把;files = relative/directory/*.ini

    改为files = /etc/supervisor/conf.d/*.conf  (该目录是存放项目conf文件的路径)

    注意:这两行前面的注释符号一定要去掉,本人在此坑被埋了很久!

    c.执行命令使配置文件生效

    supervisord -c  /etc/supervisor/supervisord.conf

    若直接执行supervisorctl reload 报错:error: <class 'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224

    可能是由于supervisord进程停止了,建议重新运行

    sudo supervisord -c /etc/supervisor/supervisord.conf
    sudo supervisorctl -c /etc/supervisor/supervisord.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.
    For help, use /usr/bin/supervisord –h

    可能是因为有一个使用supervisor配置的应用程序正在运行,需要执行supervisorctl shutdown命令终止它。

    d. 创建文件夹supervisor,在/etc/supervisor/下创建conf.d文件夹,及子文件ProjectName.conf(以项目名称命名的)

    mkdir supervisor
    cd supervior
    mkdir conf.d
    cd conf.d
    touch 项目名称.conf

    e.打开ProjectName.conf文件,添加内容如下,保存并退出:

    [program: ProjectName]
    command=dotnet ProjectName.dll //运行程序的命令
    directory=/root/Publishing/PublishOutput/ //命令执行的目录
    autorestart=true //程序意外退出是否自动重启
    autostart=true //是否自动启动
    stderr_logfile=/var/log/ProjectName.err.log //错误日志文件
    stdout_logfile=/var/log/ProjectName.out.log // 输出日志文件
    environment=ASPNETCORE_ENVIRONMENT=Production // 进程环境变量
    user=root // 进程执行的用户身份
    stopsignal=INT
    startsecs=1 //自动重启间隔 

    3、 运行supervisord,查看是否生效,执行以下命令:

    supervisord -c /etc/supervisor/supervisord.conf

     查看项目运行状态:ps -ef | grep ProjectName

    若报错:Unlinking stale socket /tmp/supervisor.sock,可以执行命令: unlink /tmp/supervisor.sock 后再执行上面的命令。

    若报错:

    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
    是因为有一个使用supervisord配置的应用程序正在运行,需要执行supervisorctl shutdown命令终止它,再执行上面的命令。

    若报错:unix:///tmp/supervisor.sock no such file   解决方法:https://ld246.com/article/1546398597198

    4、常用命令

    sudo service supervisor stop 停止supervisor服务
    sudo service supervisor start 启动supervisor服务
    supervisorctl shutdown #关闭所有任务
    supervisorctl stop|start program_name #启动或停止服务
    supervisorctl status #查看所有任务状态

    5、 配置supervisord开机启动

    a.在指定目录下

    /usr/lib/systemd/system

    创建文件touch supervisord.service,并授权chmod 755  supervisord.service

    vim /usr/lib/systemd/system/supervisord.service

    b.输入以下内容:

    [Unit]
    Description=Supervisor daemon 
    
    [Service]
    Type=forking
    ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
    ExecStop=/usr/bin/supervisorctl shutdown
    ExecReload=/usr/bin/supervisorctl reload
    KillMode=process
    Restart=on-failure
    RestartSec=42s 
    
    [Install]
    WantedBy=multi-user.target 

    保存并退出 

    执行以下命令:

    systemctl enable supervisord

    提示:

    Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.

    验证是否为开机启动:

    systemctl is-enabled supervisord

    提示:

    enabled

    表示设置成功!

    至此,创建supervisor守护进程完毕。

    6、 配置supervisord界面管理

    打开/etc/supervisor/supervisord.conf,编辑HTTP_SERVER:port、用户名、密码,保存并退出,重新加载配置:supervisorctl reload

    打开浏览器:输入ip:port,ip是supervisor安装服务器的ip地址。

    页面如下,可以对加入进程守护的服务启动管理、日志监控。

     进程管理参考:https://muzi.me/story/216/

     原文:https://www.cnblogs.com/Hai--D/p/5820718.html

  • 相关阅读:
    python读写hdf5及cdf格式文件
    常用python shell
    opencv的使用——经典大坑
    opencv python实用操作
    opencv python基本操作
    opencv c++实用操作
    opencv c++基本操作
    opencv安装
    [HNOI2005]星际贸易
    [2017SEERC]Divide and Conquer
  • 原文地址:https://www.cnblogs.com/july-1016/p/14108510.html
Copyright © 2020-2023  润新知