linux部署springboot项目的脚本
重启 restart.sh
#!/bin/bash
# 定义变量
jar_name="xxx.jar"
log_dir="logs/xxx"
# 创建日志目录
mkdir -p "$log_dir"
# 查找占用端口的进程id
pid=$(ps --no-heading -c java -f --width 1000 | grep $jar_name | awk '{print $2}')
echo "pid: ${pid:-未找到}"
echo "restarting service..."
# 停止进程
if [ -n "$pid" ]; then
echo "stopping existing process (pid: $pid)..."
kill -9 "$pid" > /dev/null 2>&1
fi
# 等待2秒
sleep 2
# 启动新进程
echo "starting application..."
nohup java -jar "$jar_name" >> "$log_dir/app.log" 2>&1 &
# 检查是否启动成功
if [ $? -eq 0 ]; then
echo "application started successfully"
echo "pid: $!"
else
echo "failed to start application"
exit 1
fi
# 保持终端不退出
read -p "press any key to continue..." -n1 -s停止 stop.sh
#!/bin/bash
jar_name="xxx.jar"
# 获取监听该端口的进程pid
pid=$(ps --no-heading -c java -f --width 1000 | grep $jar_name | awk '{print $2}')
echo "pid: ${pid:-未找到}"
if [ -z "$pid" ]; then
echo "pid not found. server may not be running."
else
echo "stopping existing process (pid: $pid)..."
kill -9 "$pid" > /dev/null 2>&1
# 检查是否成功
if [ $? -eq 0 ]; then
echo "stop success!"
else
echo "failed to stop the server."
fi
fi
read -p "press any key to continue..." -n1 -s
说明:
maven打包时,可以将脚本打包跟jar包在同一个目录
<fileset>
<directory>bin/linux</directory>
<includes>
<include>restart.sh</include>
<include>stop.sh</include>
</includes>
<outputdirectory>/</outputdirectory>
</fileset>总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论