How to Write a Shell Script for Deploying a Java Backend Application Without Docker
This guide explains how to create a shell script to deploy a Java backend application without Docker, covering variable configuration, start/stop/restart functions, log handling, and command-line usage, along with packaging and troubleshooting steps.
1. Introduction
In many development projects the client may forbid the use of Docker, requiring direct deployment on a machine. This article shows how to write a shell script to automate the deployment of a Java project (using the Ruoyi example) without Docker.
2. Writing the Shell Script
Define three configurable variables: port=9999, jar_name=blockchain.jar, and log_path=./log/$(date +%y%m%d)blockchain.log. Replace them with your own values.
#!/bin/sh
port=9999 # define variable, no spaces around =
jar_name=blockchain.jar
log_path=./log/$(date +%y%m%d)blockchain.log
# tip function
tips(){
echo "-------------------------------------"
echo ""
echo "项目端口: ${port}"
echo "项目jar目录: ${jar_name}"
echo ""
echo "你可以使用如下参数进行操作"
echo "-status -查看当前项目运行状态"
echo "-start -启动当前项目"
echo "-stop -停止当前项目"
echo "-restart -重启当前项目"
echo ""
echo "-------------------------------------"
}
# view status
status(){
pid=`netstat -apn | grep $port | awk '{print $7}' | cut -d/ -f 1`
if [ -z "${pid}" ]; then
echo "没有项目在运行"
else
echo "项目正在运行中"
fi
}
# start project
start(){
if [ -n "$1" ]; then
my_profile="$1"
fi
echo " >>> begining lauch project ${jar_name} ....."
pid=$(jps -l|grep $jar_name|cut -d ' ' -f1)
echo " >>> source project process id : $pid"
if [ $pid ]; then
echo " >>> kill source project ......"
kill -9 $pid
sleep 3
echo " >>> restart project ......"
else
echo " >>> start project......"
fi
nohup java -jar $jar_name > $log_path 2>&1 &
spin='-|/'
i=0
until [ -f $log_path ]; do
i=$(( (i+1) %4 ))
printf "\r[${spin:$i:1}]"
sleep 0.1
done
printf "
>>> tail -f $log_path
"
tail -5f $log_path
}
# stop project
stop(){
pid=`netstat -apn | grep $port | awk '{print $7}' | cut -d/ -f 1`
if [ -z "${pid}" ]; then
echo "没有项目在运行,请先启动"
else
kill -9 $pid
echo "已杀死端口为 ${port} 的应用"
fi
}
# restart project
restart(){
pid=`netstat -apn | grep $port | awk '{print $7}' | cut -d/ -f 1`
echo "正在杀死端口 ${port} 的pid ${pid} 中..."
if [ -z "${pid}" ]; then
echo "项目未启动"
else
kill -9 $pid
fi
sleep 5
start
echo "项目重启成功!"
}
# command line options
case "$1" in
"-status")
status
;;
"-start")
start
;;
"-stop")
stop
;;
"-restart")
restart
;;
*)
tips
;;
esac3. Project Packaging and Upload
Package the project into blockchain.jar and upload both the JAR and the launch script to the server. Create a log directory to store logs as shown in the accompanying screenshots.
4. Executing the Script
Run the script with the desired option, for example:
# start the project (may be slow due to Maven downloads)
sh ./launch.sh -startIf errors occur, check the log file blockchain.log in the log directory.
5. Common Commands
# check if the project started successfully
sh ./launch.sh -status
# stop the project
sh ./launch.sh -stopSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
