安装supervisor , Supervisor运行Golang守护进程

Linux · Fecmall · 于 5年前 发布 · 3303 次阅读

参考文档:在CentOS7上用Supervisor运行Golang守护进程

参考资料: https://www.jianshu.com/p/7d7c00b220bf

安装pip

如果你的linux上面没有pip,可以通过下面的步骤安装

下载pip安装文件并执行安装

[root@snails ~]# wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
[root@snails ~]# python get-pip.py
[root@snails ~]# pip -V
pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)

设置pip国内aliyun源,创建或修改配置文件

linux ~/.pip/pip.conf
windows %HOMEPATH%\pip\pip.ini

修改内容

[global]  
index-url = http://mirrors.aliyun.com/pypi/simple/  

  [install]
trusted-host=mirrors.aliyun.com  

更新pip到最新版本

[root@snails ~]# pip install -U pip

查看已安装的库

[root@snails ~]# pip list

安装supervisor

1.

[root@snails ~]# pip install supervisor

安装过程中遇到的问题:

1.1pip安装出现Command "python setup.py egg_info" failed with error code 1 的解决方案

python -m pip install --upgrade --force pip 
pip install setuptools==33.1.1

1.2安装过程中出现报错:launchpadlib 1.10.3 requires testresources, which is not installed.

通过下面的命令安装

apt install python-testresources

然后,重新执行上面的1.1步骤, 看看是否还有报错,如果1.1没有报错,重新执行pip install supervisor 完成安装

2.安装成功便可以拥有Supervisor,如果没有启动脚本,可以从 这里 下载一份,放置到 /etc/systemd/system 目录(后者优先级更高)下面便可。

mkdir -p /usr/systemd/system
wget https://raw.githubusercontent.com/Supervisor/initscripts/master/centos-systemd-etcs -O /usr/systemd/system/systemd-supervisor.service

3.通过Supervisor附送的贴心的小脚本生成默认的配置文件

echo_supervisord_conf > /etc/supervisord.conf

我们可以根据需要修改里面的配置。我这里,每个不同的项目,使用了一个单独的配置的文件,放置在 /etc/supervisor/下面。

修改 /etc/supervisord.conf ,加上如下内容:

[include]
files = /etc/supervisor/*.conf

创建golang http服务

为了测试方便,我这里用一个最简单的golang http服务。

需要搞一个golang的二进制可执行文件,里面的配置文件都需要用绝对路径,不然会报错找不到文件

// 创建log文件
touch /var/log/simple_http_server.log
chmod 777 /var/log/simple_http_server.log
// 创建 supervisor的配置文件
touch /etc/supervisor/golang.conf
// 打开文件
vim /etc/supervisor/golang.conf

加入内容

[program:golang-http-server]
command=/root/go/src/bitbucket.org/tixid/official-website-be/official-website-be  -c /root/go/src/bitbucket.org/tixid/official-website-be/config.toml
autostart=true
autorestart=true
startsecs=10
stdout_logfile=/var/log/simple_http_server.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/var/log/simple_http_server.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB

我的go的执行是上面的command: /root/go/src/bitbucket.org/tixid/official-website-be/official-website-be -c /root/go/src/bitbucket.org/tixid/official-website-be/config.toml

报错退出,然后执行

/usr/local/bin/supervisord -c /etc/supervisord.conf

执行后,如果有报错,则会直接出现报错信息

可以通过supervisorctl,查看执行的状态, 譬如:

root@ima-ThinkPad-W540:~/go/src/bitbucket.org/tixid# supervisorctlgolang-http-server               RUNNING   pid 9188, uptime 0:35:49
supervisor> 

也可以在log文件/var/log/simple_http_server.log中查看执行的log

如果出现报错:

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/local/bin/supervisord -h

则说明你已经启动了一个http服务,如果你想要重启,则需要先kill掉这个 , ps -ef | grep supervisord 查看进程号,然后kill掉即可

然后重新执行 /usr/local/bin/supervisord -c /etc/supervisord.conf 即可

supervisorctl

可以通过supervisorctl,查看执行的状态

supervisorctl是supervisord的命令行客户端管理工具,用来管理进程。

读取配置,进入交互shell环境。

supervisord -c /etc/supervisord.conf

supervisorctl
#读取配置文件
reread
#启动程序 
start amazon_server

命令说明

> status    # 查看程序状态
> stop usercenter   # 关闭 usercenter 程序
> start usercenter  # 启动 usercenter 程序
> restart usercenter    # 重启 usercenter 程序
> reread    # 读取有更新(增加)的配置文件,不会启动新添加的程序
> update    # 重启配置文件修改过的程序

也可以不进入交互环境:

$ supervisorctl status
$ supervisorctl stop usercenter
$ supervisorctl start usercenter
$ supervisorctl restart usercenter
$ supervisorctl reread
$ supervisorctl update

开机自启

1.ubuntu

ubuntu开机自启有多种方式,编辑/etc/rc.local文件,

vi /etc/rc.local

在exit 0 之前加入以下命令

/usr/local/bin/supervisord -c /etc/supervisord.conf

保存并退出。 最后修改rc.local权限

chmod +x /etc/rc.local

2.centos

复制文件,将/usr/systemd/system/systemd-supervisor.service复制到如下文件

cp /usr/systemd/system/systemd-supervisor.service /usr/lib/systemd/system/

启动服务

systemctl enable supervisord

验证一下是否为开机启动

systemctl is-enabled supervisord
共收到 0 条回复
没有找到数据。
添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics