参考文档:在CentOS7上用Supervisor运行Golang守护进程
安装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
launchpadlib 1.10.3 requires testresources, which is not installed.
通过下面的命令安装
apt install python-testresources
pip install 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
/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>
/var/log/simple_http_server.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
ps -ef | grep supervisord
/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
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