当前位置: 移动技术网 > 网络运营>服务器>Linux > Linux脚本自动打包发服务

Linux脚本自动打包发服务

2020年03月09日  | 移动技术网网络运营  | 我要评论

适用于前后台分离的服务

一个jar包 一个前台工程

实现自动杀端口,复制文件,备份数据,发布新版本。

脚本复制到linux中

【./jb.sh】按enter运行即可

内容如下:

#自定义存放的地方
#文件存放的根目录
latestdir=/home/xnj
#jar包名字
jarname=swweb.jar
#前端压缩包文件名
qdzipname=desktop.zip
#前端tomcat的位置
qdtomcatposition=/home/tomcat/apache-tomcat-8.5.43/webapps/root
#脚本存放的位置
jbfileposition=/home/jb
#结束
#年
year=katex parse error: expected 'eof', got '#' at position 12: (date +%y) #̲月 month=(date +%y%m)
#日
day=katex parse error: expected 'eof', got '#' at position 17: …date +%y%m%d); #̲时分秒 hms=(date +%h%m%s)
#当前目录
currentdir=$(pwd)
#cd到当前目录
cd $jbfileposition
#创建年目录
if [ ! -d $latestdir ]; then
mkdir $latestdir
fi
if [ ! -d latestdir/ latestdir/latestdir/year ]; then
mkdir latestdir/ latestdir/latestdir/year
fi
if [ ! -d latestdir/ latestdir/latestdir/year/$month ]; then
mkdir latestdir/ latestdir/latestdir/year/$month
fi
if [ ! -d latestdir/ latestdir/latestdir/year/month/ month/month/day ]; then
mkdir latestdir/ latestdir/latestdir/year/month/ month/month/day
fi
if [ ! -d latestdir/ latestdir/latestdir/year/month/ month/month/day/$hms ]; then
mkdir latestdir/ latestdir/latestdir/year/month/ month/month/day/katex parse error: expected 'eof', got '#' at position 9: hms fi #̲将当前目录的下的jar包和前端…jarname latestdir/ latestdir/latestdir/year/month/ month/month/day/hmscp−r./ hmscp -r ./hmscp−r./qdzipname latestdir/ latestdir/latestdir/year/month/ month/month/day/$hms
#杀掉进程 awk是 是处理行数据的 xargs 是多个命令的连接的管道
ps -ef | grep $jarname | grep -v grep | awk ‘{print $2}' | xargs kill -9
#删除前台工程
rm -rf qdtomcatposition/ qdtomcatposition/qdtomcatposition/qdzipname
rm -rf $qdtomcatposition/static
rm -rf katex parse error: expected 'eof', got '#' at position 29: …ion/ #̲加载新前台工程 cp -r .…qdzipname $qdtomcatposition/
cd qdtomcatpositionunzip./ qdtomcatpositionunzip ./qdtomcatpositionunzip./qdzipname
#启动新的jar包
nohup java -jar latestdir/ latestdir/latestdir/year/month/ month/month/day/hms/ hms/hms/jarname >nohup.out &

ps:服务器自动打包发布shell脚本

脚本功能简介

  • 自动拉取git服务器最新代码
  • 服务器maven自动打包
  • 自动重启发布jar项目
  • nohup 启动项目后保存进程号至文件

前置条件

  • git可用
  • maven可用
  • spring boot打成jar包可以正常启动

脚本内容

#!/bin/sh
echo " ====开始拉取仓库最新代码==== "
cd /data/code/better;pwd;
git pull;git status;
echo "   "
git log --pretty=format:"%h - %an, %ar : %s" -5;
echo " ====服务器打包===="
mvn install -dmaven.test.skip=true;
mvn clean package -dmaven.test.skip=true;
echo " ====移动jar包并改名===="
cd /data/app;pwd;
cp /data/code/better/backend_web/target/backend_web-1.0-snapshot.jar /data/app;
mv backend_web-1.0-snapshot.jar better.jar;
echo " =====停止java应用======"
#jps | grep better.jar | awk '{print $1;}' | xargs kill -9
kill -9 `cat /data/app/betterpid.txt`
echo " =====启动java应用======"
nohup java -jar better.jar >/data/logdir/better/out.log & echo $! > /data/app/betterpid.txt &
#查看日志
echo "   "
echo " ===启动项目的进程号====";
cat /data/app/betterpid.txt;
tail -20f /data/logdir/better/out.log;

运行脚本

sh better.sh

总结

以上所述是小编给大家介绍的linux脚本自动打包发服务,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网