#!/bin/bash # 定义路径和变量 JAR_PROCESS_NAME="java" JAR_FILE_PATH="/root/dfsz/jars/gw-dz-core.jar" # 请确认此路径是你的Jar文件的实际路径 LOG_FILE="/root/dfsz/jars/restartBash.log" #脚本日志文件 FIND_LOG_FILE="/root/dfsz/jars/log.log" #要查询的日志文件 # 初始化重启次数 RESTART_COUNT=0 # 定义启动 Jar 进程的函数 function start_jar { nohup java -Xms8g -Xmx8g -jar $JAR_FILE_PATH --spring.profiles.active=prod_92 > log.log 2>&1 & echo -e "[$(date)] Jar process started.\n" >> $LOG_FILE # 记录重启次数和时间到重启日志 RESTART_COUNT=$((RESTART_COUNT + 1)) echo -e "[$(date)] Jar process restarted. Total restarts: $RESTART_COUNT\n" >> $LOG_FILE } # 定义监控 Jar 进程的函数 function monitor_jar { while true; do # 检查是否有 Java 进程运行 if ! pgrep -f $JAR_PROCESS_NAME > /dev/null; then echo -e "[$(date)] Jar process not found, restarting...\n" >> $LOG_FILE start_jar fi # 检查日志文件中是否包含内存溢出错误 if grep -q "java.lang.OutOfMemoryError" $FIND_LOG_FILE; then echo -e "[$(date)] Detected OutOfMemoryError, restarting jar...\n" >> $LOG_FILE pkill -f $JAR_PROCESS_NAME start_jar # 清空日志文件以避免重复处理相同错误 #: > $LOG_FILE fi # 检查日志文件中是否包含内存溢出错误 if grep -q "state should be: open" $FIND_LOG_FILE; then echo -e "[$(date)] Detected mongo: state should be: open, restarting jar...\n" >> $LOG_FILE pkill -f $JAR_PROCESS_NAME start_jar # 清空日志文件以避免重复处理相同错误 #: > $LOG_FILE fi # 等待10秒再检查 sleep 30 done } # 启动监控 monitor_jar
后台启动脚本
nohup /root/dfsz/jars/restartBash.sh > /dev/null 2>&1 &
查看脚本是否启动
ps -ef|grep sh
标签:JAR,LOG,Jar,jar,报错,FILE,linux,日志 From: https://www.cnblogs.com/Donnnnnn/p/18572612