#!/bin/bash
# 设置颜色变量
GREEN='\033[32m'
RED='\033[31m'
NC='\033[0m' # No Color
# 定义服务数组
services=(
"system"
"xxljob"
"inte"
"monitor"
"gateway"
)
# 定义服务对应的JAR文件名
declare -A service_jars=(
["system"]="jeecg-system-cloud.jar"
["xxljob"]="jeecg-cloud-xxljob.jar"
["inte"]="jeecg-cloud-inte.jar"
["monitor"]="jeecg-cloud-monitor.jar"
["gateway"]="jeecg-cloud-gateway.jar"
)
# 显示菜单
echo -e "${GREEN}请选择需要重启的服务:${NC}"
for i in "${!services[@]}"; do
echo -e "${GREEN}$((i+1)). ${services[i]}${NC}"
done
echo -e "${GREEN}0. 全部重启${NC}"
read -p "请输入选项 (0-${#services[@]}):" choice
# 检查输入是否有效
if [[ ! $choice =~ ^[0-9]+$ ]] || (( choice < 0 || choice > ${#services[@]} )); then
echo -e "${RED}无效的选项,请重新运行脚本并选择有效的选项。${NC}"
exit 1
fi
# 定义重启函数
restart_service() {
local service=$1
local jar_file=${service_jars[$service]}
local tem_path="/mnt/api/tem/${jar_file}"
local dest_path="/mnt/api/${jar_file}"
# 停止服务
echo -e "${GREEN}停止 ${service}...${NC}"
pids=$(ps aux | grep "[j]eecg" | grep "$jar_file" | awk '{print $2}')
if [ -n "$pids" ]; then
for pid in $pids; do
kill -9 $pid
done
echo -e "${GREEN}${service} 的所有进程已终止。${NC}"
else
echo -e "${RED}${service} 没有正在运行的进程。${NC}"
fi
# 等待进程完全终止
sleep 2
# 移动备份文件
echo -e "${GREEN}移动 ${service} 的 JAR 文件到目标目录...${NC}"
mv -b "$tem_path" "$dest_path"
if [ $? -eq 0 ]; then
echo -e "${GREEN}JAR 文件移动成功。${NC}"
else
echo -e "${RED}JAR 文件移动失败。${NC}"
exit 1
fi
# 启动服务
echo -e "${GREEN}启动 ${service}...${NC}"
nohup java -Xms512m -Xmx1024m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m -jar "$dest_path" --spring.profiles.active=prod > "/mnt/api/logs/${service}.log" &
if [ $? -eq 0 ]; then
echo -e "${GREEN}${service} 已成功启动。${NC}"
else
echo -e "${RED}${service} 启动失败。${NC}"
exit 1
fi
}
# 根据用户选择执行相应操作
if [ "$choice" -eq 0 ]; then
echo -e "${GREEN}全部重启...${NC}"
for service in "${services[@]}"; do
restart_service "$service"
done
else
selected_service=${services[$((choice-1))]}
echo -e "${GREEN}重启 ${selected_service}...${NC}"
restart_service "$selected_service"
fi
echo -e "${GREEN}重启脚本执行完成。${NC}"
标签:service,重启,NC,jar,echo,GREEN,linux,services
From: https://www.cnblogs.com/qwer78/p/18650383