Linux script auto package service

Time:2020-10-16

Services for front and back separation

One jar package, one foreground project

It can automatically kill ports, copy files, backup data and release new versions.

Script copied to Linux

【./ jb.sh 】Press enter to run

The contents are as follows:

#Custom storage location
#The root directory where the file is stored
latestDir=/home/xnj
#Jar package name
jarName=swWeb.jar
#Front end zip file name
qdZipName=Desktop.zip
#Location of front end Tomcat
qdTomcatPosition=/home/tomcat/apache-tomcat-8.5.43/webapps/ROOT
#Location of script
jbFilePosition=/home/jb
#End
#Year
Year = KAtex parse error: expected 'EOF', got 'ා' at position 12: (date +% y) { month = (date +% Y% m)
#Japan
day=KaTeX parse error: Expected 'EOF', got '#' at position 17: … Date +% Y% m% d); (date +% H% m% s)
#Current directory
currentDir=$(pwd)
#CD to current directory
cd $jbFilePosition
#Create year directory
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 / lastdir / year / month / month / month / day / KAtex parse error: expected 'EOF', got 'ා' at position 9: HMS fi 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
#Killing the process awk is the processing line data, xargs is the connection pipeline of multiple commands
ps -ef | grep $jarName | grep -v grep | awk ‘{print $2}' | xargs kill -9
#Delete foreground project
rm -rf qdTomcatPosition/ qdTomcatPosition/qdTomcatPosition/qdZipName
rm -rf $qdTomcatPosition/static
rm -rf KaTeX parse error: Expected 'EOF', got '#' at position 29: … ion/ index.html  #Loading the new front desk project cp-r qdZipName $qdTomcatPosition/
cd qdTomcatPositionunzip./ qdTomcatPositionunzip ./qdTomcatPositionunzip./qdZipName
#Start the new jar package
nohup java -jar latestDir/ latestDir/latestDir/year/month/ month/month/day/hms/ hms/hms/jarName >nohup.out &

PS: server automatically packages and publishes shell scripts

Introduction to script function

  • Automatically pull the latest code of GIT server
  • Server Maven automatic packaging
  • Automatically restart publishing jar project
  • After nohup starts the project, save the process number to the file

Preconditions

  • Git available
  • Maven available
  • Spring boot can be started normally if it is packaged into jar package

Script content

#!/bin/sh
Echo "= = = = start fetching the latest code of warehouse = = = ="
cd /data/code/better;pwd;
git pull;git status;
echo "   "
git log --pretty=format:"%h - %an, %ar : %s" -5;
Echo "= = = = server packaging = = = ="
mvn install -Dmaven.test.skip=true;
mvn clean package -Dmaven.test.skip=true;
Echo "= = = = = move jar package and rename = = = ="
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 "= = = = stop Java application = = = ="
#jps | grep better.jar | awk '{print $1;}' | xargs kill -9
kill -9 `cat /data/app/betterPid.txt`
Echo "= = = = start Java application = = = ="
nohup java -jar better.jar >/data/logdir/better/out.log & echo $! > /data/app/betterPid.txt &
#View log
echo "   "
Echo "= = = = the process number of the startup project = = = =";
cat /data/app/betterPid.txt;
tail -20f /data/logdir/better/out.log;

Run script

sh better.sh

summary

The above is the Linux script automatic package and send service introduced by Xiaobian. I hope it can help you. If you have any questions, please leave me a message, and the editor will reply to you in time. Thank you very much for your support to the developeppaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!