banner
NEWS LETTER

Go 程序发版脚本 - supervisor

Scroll down

安装 supervisor (已安装可跳过)

  • 环境 ubuntu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# ubuntu 安装
apt install -y supervisor

# 开机自启
systemctl enable supervisor

# 开始运行
systemctl start supervisor

# 查看服务状态
systemctl status supervisor

# 查看是否存在进程
ps -aux | grep supervisor

# supervisord 的配置文件默认位于 /etc/supervisord.conf
ls /etc/supervisord.conf

日志打印脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
BL="\033[36m" # 日志消息
GR="\033[32m" # 成功消息
YL="\033[33m" # 告警消息
RD="\033[31m" # 错误消息
PL='\033[0m'

LOG() {
echo -e "${BL}[LOG]$1${PL}"
}
INFO() {
echo -e "${GR}[INFO]$1${PL}"
}
ERROR() {
echo -e "${RD}[ERROR]$1${PL}"
}
WANF() {
echo -e "${YL}[WANF]$1${PL}"
}

go 打包脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
go_build_by_linux() {
# 构建Linux版本
export GOOS=linux
export GOARCH=amd64

go build -o ${输出名} ${程序所在目录}

unset GOOS
unset GOARCH
}

go_build_by_windows() {
# 构建Windows版本
export GOOS=windows
export GOARCH=amd64

go build -o $1.exe $2

unset GOOS
unset GOARCH
}

supervisor 模板输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 用于 supervisor 管理进程
function output() {
echo \
"[program:${app_name}] ;
directory=${app_dir} ;
command=${app_dir}/${app_name} ;
autostart=true ;
autorestart=true ;
startsecs=10 ;
startretries=3 ;
user=root ;
redirect_stderr=true ;
stdout_logfile=${log_dir}/${app_name}_stdout.log ;
stdout_logfile_maxbytes=20MB ;
stdout_logfile_backups=20 ;
" >${app_name}.ini
}

# 启动命令
supervisorctl start ${app_name} ;

# 更新命令
supervisorctl update ;

# 重启命令
supervisorctl restart ${app_name} ;

# 查看状态
supervisorctl status ${app_name} ;

打包

1
2
3
4
5
6
7
8
9
10
# 打包
function tar_pack() {
# 可以配制多个 [原文件名/目录名] 打成一个包
tar -Jcvf ${目标文件名}.tar.xz --exclude=".*" [${原文件名/目录名} ...]
}

# 解包
function tar_extract() {
tar -Jxvf [原文件名].tar.xz
}

scp

1
2
3
4
5
6
7
function upload(){
sshpass -p${password} scp -P${port} ${本地文件名} ${user}@${ip}:${远程目录}
}

function download(){
scp ${user}@${ip}:${远程文件} ${本地目录}
}

ssh 执行远端命令

1
2
3
function do_ssh_command() {
sshpass -p ${password} ssh -P${port} ${user}@${ip} '[command]'
}

创建目录

1
2
3
4
5
6
mkdirIfNotExist() {
stat ${目录}
if [ $? = 1 ]; then
mkdir -p "${目录}"
fi
}

示例-组合脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 先创建临时目录
mkdirIfNotExist ${临时目录}

# 打包 Go 程序
go_build_by_linux ${输出名} ${程序所在目录}

# 输出 supervisor 管理进程模板
output ${app_name} ${app_dir} ${log_dir}

# 将Go程序包和supervisor管理进程模板放入临时目录
# 将临时目录打包
tar_pack ${目标文件名} ${临时目录}

# 上传至目标机器
upload

# 进入目标机器
# 解压包
# 分发内容到对应目录
# supervisor 更新 启动
do_ssh_command ${命令}

你那么好看,应该会支持一下我吧~

其他文章
cover
Go Debug 工具
  • 23/11/23
  • 17:39
  • golang
目录导航 置顶
  1. 1. 安装 supervisor (已安装可跳过)
  2. 2. 日志打印脚本
  3. 3. go 打包脚本
  4. 4. supervisor 模板输出
  5. 5. 打包
  6. 6. scp
  7. 7. ssh 执行远端命令
  8. 8. 创建目录
  9. 9. 示例-组合脚本
请输入关键词进行搜索