go web 一键发布服务

go web一键上传程序并执行服务脚本

即将大四学生党一枚,个人学习golang web中,工作中遇到客户总改需求,经常需要重新编译打包上传,关闭旧服务,启动新服务……脚本便诞生了

项目地址:https://github.com/Dreamlu/deercoder-go.sh

原理:

  • shell命令
  • scp远程复制(liunx通用命令)
  • expect进行交互输入密码(免密登录方式,每次重装系统后都要重新弄,本人喜欢折腾..em…弃)
  • ssh
  • expect的spawn执行线上脚本
  • go build打包编译
  • 新旧进程的替换pid

运行:
复制 upload.sh、deercoder.sh到go web 根目录下
./upload.sh
详情:
upload.sh(替换ip、password 、uploadDir(上传目录))

#!/bin/bash
#upload.sh made by lucheng
#server ip and password
ip=
password=
uploadDir=root
#get the newest go build
go build
#directory
curDir=$(basename `pwd`)
#upload current directory and file
#$echo $password | scp -r `pwd` root@$ip:/$uploadDir/
#expect
expect<<EOF
set timeout 200
spawn scp -r `pwd` root@$ip:/$uploadDir/
expect "password:" 
send "$password\r"
expect eof

#try to run script
spawn ssh root@$ip "
echo 'upload over,try to run progress script deercoder.sh';
cd /$uploadDir/$curDir;
./deercoder.sh "
expect "password:" 
send "$password\r"
expect eof
EOF

deercoder.sh(go web 执行脚本)

#!/bin/bash
#deercoder.sh made by lucheng
#if some error,connect me:wechat:a_862362681
#get programe's name the same as directory
curDir=$(basename `pwd`)
echo "current dir's or programe's name is $curDir"

#execute the programe
#delete old process
pid=$(ps aux | grep $curDir | grep -v grep | awk '{print $2}')
if [ "$pid" ]
then
    echo "$curDir programe is running, pid is $pid"
    echo "the script will try to kill it and start new progress"
    # delete this old process
    kill -9 $pid
else
    echo "no old process"
fi

#start new daemon   
nohup ./$curDir &
pid=$(ps aux | grep $curDir | grep -v grep | awk '{print $2}')
if [ "$pid" ]
then
    echo "the programe's new pid id $pid"
else
    echo "no such process: $curDir"
    echo "please try to compile and upload it"
    #go build
    #echo "is tring to execute the script itself"
    #execute current script file
    #./$0
fi

线上测试:
这里写图片描述
最终结果:
这里写图片描述

注:服务器如有go环境可以去掉deercoder.sh最后几行的注释,可以在未编译或编译出错时,线上进行编译执行服务
欢迎关注,go web脚本更新,如有需要请联系我,star一下,猿一枚~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值