Java jar包程序 启动停止脚本 shell bash
启动
启动时 可指定前缀(名称)
start.sh
#!/bin/bash
# 使用时直接运行# 寻找当前目录下后缀为 .jar 的文件
#options=($(find . -maxdepth 1 -type f -name "*.jar"))
# 寻找当前目录下后缀为 .jar 的文件,并按时间倒序排序
options=($(find . -maxdepth 1 -type f -name "*.jar" -exec stat -c "%Y %n" {} + | sort -r -n | awk '{print $2}'))
# 检查是否存在 .jar 文件
if [ ${#options[@]} -eq 0 ]; thenecho "错误:当前目录下不存在 .jar 文件。"exit 1
fi# 打印选项
for i in "${!options[@]}"; doecho "$((i+1)). ${options[i]}"
done# 循环校验选选择
while true; do# 清除终端屏幕#clear# 提示用户输入序号read -p "请输入序号进行选择: " choice# 检查输入是否为正整数#if [[ ! $choice =~ ^[1-9][0-9]*$ ]]; then# echo "无效的输入,请输入一个有效的序号。"# continue#fi# 检查输入是否为空或非数字if [[ -z "$choice" || ! "$choice" =~ ^[0-9]+$ ]]; thenecho "错误:请输入有效数字。"continuefi# 将输入转换为索引index=$((choice - 1))# 检查索引是否在有效范围内if [[ $index -lt 0 || $index -ge ${#options[@]} ]]; thenecho "无效的序号,请输入一个有效的序号。"continuefiselected_item=${options[index]}# 打印选中的选项echo ""echo "您选择了:$selected_item"# 执行命令nohup $JAVA_HOME_17/bin/java -jar -Dspring.profiles.active=prod -Dfile.encoding=UTF-8 -Djava.security.egd=file:/dev/urandom "$selected_item" > /dev/null 2>&1 &echo "已执行 $selected_item"# 再校验一次ps -ef | grep "$selected_item" | grep -v grepbreak
doneexit 0
停止
可指定 前缀
选择要停止的java jar 进程
尝试10次 每次间隔1秒
若仍未停止可选择强制停止
停止后会打印输出提示该进程已停止
stop.sh
#!/bin/bash# 检查是否提供了 app_prefix 参数
if [ -z "$1" ]; thenapp_prefix="" # 设置默认值
elseapp_prefix="$1"
fi# 运行 ps 命令并通过管道筛选出匹配的进程
# processes=$(ps -ef | grep "$app_prefix.*.jar" | grep -v grep)
processes=$(ps -ef | grep "[j]ava.*\b${app_prefix}[^/]*\.jar\b")# 检查是否存在匹配的进程
if [ -z "$processes" ]; thenecho "没有找到匹配的进程。"exit 1
fi# 打印选项
options=()
while IFS= read -r line; dopid=$(echo "$line" | awk '{print $2}')options+=("$pid")# 假设进程信息中包含了完整的jar路径,使用awk或其它方式提取jar包名jar_name=$(echo "$line" | grep -oP '(?:.*/)?\K[\w.-]+\.jar(?=\s|$)') # 如果直接获取jar名称有困难,请根据实际情况调整正则表达式或处理逻辑if [[ -z "$jar_name" ]]; thenjar_name="unknown jarName" # 如果没有找到jar包名,则设置为"N/A"fiecho "${#options[@]}. $pid -- $jar_name -- $line"
done <<< "$processes"# 循环校验选择
while true; doread -p "请输入序号进行选择: " choice# 检查输入是否为空或非数字if [[ -z "$choice" || ! "$choice" =~ ^[0-9]+$ ]]; thenecho "错误:请输入有效数字。"continuefi# 将输入转换为索引index=$((choice - 1))# 检查索引是否在有效范围内if [[ $index -lt 0 || $index -ge ${#options[@]} ]]; thenecho "无效的序号,请输入一个有效的序号。"continuefiselected_pid="${options[index]}"echo ""echo "您选择了进程 PID:$selected_pid"# 发送 SIGTERM(15)信号给选中的进程kill -15 "$selected_pid"echo "已发送 SIGTERM (15) 给进程 $selected_pid,正在等待其终止..."# 等待并检查进程是否终止attempts=0# 尝试 10 次(可自定义)max_attempts=10# 等待1秒(可自定义)interval=1while [ $attempts -lt $max_attempts ]; doif kill -0 "$selected_pid" 2>/dev/null; thensleep "$interval"attempts=$((attempts + 1))elseecho "进程 $selected_pid 已成功终止。"break 2fidone# 若仍未终止,询问是否强制终止if kill -0 "$selected_pid" 2>/dev/null; thenread -p "进程 $selected_pid 未响应 SIGTERM,是否强制终止?(y/n): " confirmcase "$confirm" iny|Y|yes|YES)kill -9 "$selected_pid"sleep 1if kill -0 "$selected_pid" 2>/dev/null; thenecho "错误:无法强制终止进程 $selected_pid。请手动处理。"exit 1elseecho "进程 $selected_pid 已被强制终止。"fi;;n|N|no|NO)echo "您选择不强制终止进程 $selected_pid。"exit 0;;*)echo "输入无效,请输入 y 或 n。"continue;;esacelseecho "进程已正常终止。"fibreak
doneexit 0