几个SpringBoot框架web程序部署简单脚本
脚本一(通用)
一个通用的SpringBoot jar包部署脚本。提供通用的停止、运行、重启、代码备份功能
#!/usr/bin/bash
usage(){
echo usage ./shellfile option appfilename
echo options:[stop/start/retsart/backup]
}
appstop(){
if [ -f $1 ]
then
echo to stop app with:$1
filename=$1
pids=`jps -mlvV | grep ${filename##*/} | awk '{print $1}'`
for i in $pids
do
kill $i
if [ $? == 0 ]
then
echo quiet exit ok!
else
kill -9 $i
echo force exit ok!
fi
done
else
echo file:$1 is not found
fi
}
appstart(){
if [ -f $1 ]
then
filename=$1
#后台运行java程序
pid=`nohup java -jar ${filename##*/} > ./nohup.out 2>&1 &`
if [ $? == 0 ]
then
echo ok
else
echo error
fi
else echo file:$1 is not found
fi
}
backup(){
if [ -f $1 ]
then
now=`date '+%Y%m%dT%H%M%S'`
`cp $1 $1.back.$now`
else
echo file:$1 is not found
fi
}
apprestart(){
filename=$1
# 找到目标进程 截取文件名称 获取PID
pids=`jps -mlvV | grep ${filename##*/} | awk '{print $1}'`
if [ $pids ]
then
appstop $filename
#脚本暂停5秒
sleep 5
appstart $filename
else
appstart $filename
fi
}
case $1 in
stop) appstop $2
;;
start) appstart $2
;;
restart) apprestart $2
;;
backup) backup $2
;;
*) usage
;;
esac
nohup: 重定向标准错误到标准输出:https://blog.csdn.net/x18094/article/details/106526738
shell字符串截取函数参考:http://c.biancheng.net/view/1120.html
shell日期格式化参考:https://blog.csdn.net/stone_tomcate/article/details/122193554
脚本二(高度定制)
主要功能:快速部署公司内部基于springboot框架项目的jar包,提供jar包的运行、停止、重启、代码备份、代码的部署功能。
#!/usr/bin/bash
filename="tunnel-device-monitor-test.jar"
port="8584"
samplingServerAddr="http://192.168.2.168:9977"
dburl="jdbc:mysql://192.168.2.201:3306/intelligent_fire_control?serverTimezone=Asia/Shanghai&useSSL=false"
dbusername="root"
dbpassword="JT_passw0rd"
judge(){
if [ $1 != 0 ]
then
echo 部署程序异常
exit 1
fi
}
stop_app(){
pid=`ps -aux | grep $filename | awk '{if($10!="0:00"){print $2;}}'`
echo 正在停止目标服务进程${filename} PID:$pid
kill $pid
if [ $? == 0 ]
then
echo "停止成功"
else
kill -9 $pid
echo "强制结束成功"
fi
}
backup_file(){
cd code_back_dir 2> /dev/null
if [ $? == 1 ]
then
mkdir code_back_dir
else
cd ..
fi
now=`date '+%Y年%m月%d日%H时'`
cp ${filename} ./code_back_dir/${filename}.back.${now}
rm -rf ${filename}
echo 代码备份成功
}
code_backup(){
filename=$1
cd code_back_dir 2> /dev/null
if [ $? == 1 ]
then
mkdir code_back_dir
else
cd ..
fi
now=`date '+%Y年%m月%d日%H时'`
cp ${filename} ./code_back_dir/${filename}.back.${now}
echo 代码备份成功
}
rename_file(){
mv $1 $filename
}
run_app(){
echo 开始启动服务... $filename
nohup java -D"server.port"=$port -D"sampling.serverAddr"=$samplingServerAddr -D"spring.datasource.url"=$dburl -D"spring.datasource.username"=$dbusername -D"spring.datasource.password"=$dbpassword -jar $filename > ./nohup.out 2>&1 &
if [ $? == 0 ]
then
echo "服务启动成功..."
else
echo "服务启动失败..."
fi
}
deploy(){
stop_app
judge $?
sleep 1
backup_file
judge $?
sleep 1
rename_file $1
judge $?
sleep 3
run_app
judge $?
}
restart_app(){
stop_app
judge $?
sleep 2
run_app
judge $?
}
usage(){
echo command option
echo " 可选项:"
echo " 重新部署[deploy jar名称]"
echo " 停止运行[stop]"
echo " 开始[start]"
echo " 重启[restart]"
echo " 代码备份[backup jar名称]"
}
case $1 in
deploy)
if [ $2 ]
then
deploy $2
else
echo 参数缺失,停止部署,请检查后重试
fi
;;
stop) stop_app
;;
start) run_app
;;
restart) restart_app
;;
backup)
if [ $2 ]
then
code_backup $2
else
echo 参数缺失,停止备份..
fi
;;
*) usage
;;
esac
脚本中断、退出参考:https://blog.csdn.net/ganfanren00001/article/details/122741241
脚本三(docker)
作用:快速部署一个tomcat环境
#!/usr/bin/bash
data_volume_dir=$1
port=$2
deploy_type=$3
war_dir="war_dir"
interner_war_dir="/usr/local/tomcat/webapps"
log_dir="log_dir"
interner_log_dir="/usr/local/tomcat/logs"
container_interner_port="8080"
contain_name="tomcat_${port}"
`cd ${data_volume_dir}`
data_volume_dir=`pwd`
image="tomcat:jre11-openjdk-slim-buster"
image_prefix=${image%%:*}
image_suffix=${image##*:}
usage(){
echo "脚本用法:"
echo "请输入:./脚本.sh 文件夹绝对路径 端口 模式(临时tomcat服务[--rm],持久tomcat服务[--restart=always])"
echo "示例:./deploy_tomcat_by_docker.sh . 8888 --rm/--restart=always"
}
inspect_port(){
echo 端口占用检测...
`ss -ntlp | awk '{print $4}' | grep $port`
if [ $? == 0 ]
then
echo 端口:${port}被占用,请更换端口...
exit 1
fi
#min=`sysctl net.ipv4.ip_local_port_range | awk '{print $3}'`
#max=`sysctl net.ipv4.ip_local_port_range | awk '{print $4}'`
min=6669
max=65534
if [ $port -lt $min -o $port -gt $max ]
then
echo 非法的端口${port},请更换端口号
exit 1
fi
`mkdir ${contain_name}`
}
inspect_docker_image_exist(){
echo 检测目标镜像在本地是否存在
docker images | grep $image_prefix | grep $image_suffix
if [ $? == 1 ]
then
echo 目标镜像不存在,开始拉取远程镜像....
`docker pull $image`
echo 未知异常,请检查错误并重试...
exit 1
fi
}
deploy_tomcat(){
echo 开始创建目标tomcat服务... $contain_name
docker run ${deploy_type} -d -p ${port}:${container_interner_port} --name ${contain_name} -v ${data_volume_dir}/${contain_name}/${war_dir}:${interner_war_dir} -v ${data_volume_dir}/${contain_name}/${log_dir}:${interner_log_dir} $image
if [ $? == 0 ]
then
echo 容器部署成功...
echo 开始创建测试环境...
cd ${contain_name}/${war_dir}
mkdir ROOT
cd ROOT
wget -O test.jpg http://www.pp3.cn/uploads/20120418lw/13.jpg
echo "访问容器测试地址:"
echo "http://`ifconfig -a | grep inet* | grep broadcast | grep -v 172 | awk '{print $2}'`:${port}/test.jpg"
echo "如无法正常访问,1.请检查防火墙是否开放端口。2.是否使用了预定义内置服务端口(https://blog.csdn.net/xixiyuguang/article/details/107556211)"
else
echo 容器部署失败
fi
}
if [ $# -lt 3 ]
then
echo 参数异常,结束进程
usage
exit 1
else
inspect_port
inspect_docker_image_exist
deploy_tomcat
fi
参考文章:
Linux-Shell脚本中获取本机ip地址方法:https://blog.csdn.net/weixin_38556197/article/details/121134600