- env.sh
定义公共函数,变量,服务,路径等
#!/usr/bin/env bash
# shellcheck disable=SC2034
project_root=$(pwd)"/../"
api_binary_root="${project_root}bin"
logs_root="${project_root}logs"
# service list
srvs=("srv_api" "srv_cron" "srv_notify" "srv_oss")
# app dirs
src_dirs=("api" "cron" "notify" "oss")
Upgrade(){
# shellcheck disable=SC2164
cd "${project_root}"
git checkout -- ./
git pull
res=$?
if [ $res -ne 0 ]; then
echo "Err:git pull src source fail"
echo
exit 1
fi
echo "upgrade project source success"
set -x
chmod +x scripts/*
set +x
# shellcheck disable=SC2164
cd "${project_root}src/proto"
git checkout -- ./
git pull
res=$?
if [ $res -ne 0 ]; then
echo "Err:git pull proto source fail"
echo
exit 1
fi
echo "upgrade proto source success"
# shellcheck disable=SC2164
cd "${project_root}"
swag init -g "./src/api/srv_api.go"
res=$?
if [ $res -ne 0 ]; then
echo "Err:generate srv_api docs fail"
echo
exit 1
fi
echo "generate srv_api docs success"
}
# shellcheck disable=SC2164
Build(){
cd "${project_root}/src/${1}"
make install
res=$?
if [ $res -ne 0 ]; then
echo "$1 make install error!"
echo
exit 1
fi
echo "$1 make install binary success"
}
ReStartSrv(){
#Check if the service exists,If it is exists,kill this process
# shellcheck disable=SC2126
check=$(ps aux | grep -w ${1} | grep -v grep | wc -l)
if [ "$check" -ge 1 ]; then
# shellcheck disable=SC2009
oldPid=$(ps aux | grep -w ${1} | grep -v grep | awk '{print $2}')
kill -9 "${oldPid}"
fi
#Waiting port recycling
sleep 1
# shellcheck disable=SC2164
# shellcheck disable=SC2154
cd "${project_root}"
# shellcheck disable=SC2154
if [ ! -d "${logs_root}" ];then
mkdir "${logs_root}"
fi
# shellcheck disable=SC2154
nohup "${api_binary_root}"/${1} >>"${logs_root}"/${1}.log 2>&1 &
#Check launched service process
sleep 1
# shellcheck disable=SC2126
check=$(ps aux | grep -w ${1} | grep -v grep | wc -l)
allPorts=""
if [ "$check" -ge 1 ]; then
# shellcheck disable=SC2009
allNewPid=$(ps aux | grep -w ${1} | grep -v grep | awk '{print $2}')
for i in $allNewPid; do
ports=$(netstat -netulp | grep -w "${1}" | awk '{print $4}' | awk -F '[:]' '{print $NF}')
allPorts=${allPorts}"$ports "
done
echo -e "SERVICE START SUCCESS "
echo -e "SERVICE_NAME: "${1}
echo -e "PID: ""${allNewPid}"
echo -e "LISTENING_PORT: ""${allPorts}"
else
echo -e "SERVICE START ERROR !!! PLEASE CHECK ERROR LOG"
fi
}
if [ "$1" = "up" ]; then
Upgrade
fi
- build.sh
编译生成二进制文件
#!/usr/bin/env bash
source env.sh
# shellcheck disable=SC2154
for src in ${src_dirs[*]}
do
Build "${src}"
done
- restart.sh
更新代码,编译生成二制制文件,最后重启服务
#!/usr/bin/env bash
source env.sh
if [ "$1" = "up" ]; then
# shellcheck disable=SC2154
for src in ${src_dirs[*]}
do
Build "${src}"
done
fi
# shellcheck disable=SC2154
for srv in ${srvs[*]}
do
ReStartSrv "$srv"
done