简单sh文件运行和停止jar

1、运行spring项目

#!/bin/sh
APP="com.xx.BestApp"
PROC_NAME="BestApp"

CLASS_PATH="./classes:./lib/*:./config:./*"

_current_path() {
    SOURCE=${BASH_SOURCE[0]}
    DIR=$( dirname "$SOURCE" )
    while [ -h "$SOURCE" ]
    do
        SOURCE=$(readlink "$SOURCE")
        [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
        DIR=$( cd -P "$( dirname "$SOURCE"  )" && pwd )
    done
    DIR=$( cd -P "$( dirname "$SOURCE" )" && pwd )
    echo $DIR
}

#start shell
start_run(){
   echo "Application starting"
   exec -a $PROC_NAME java -jar bestApp-1.0.0.jar --spring.config.location=./application.yml -XX:+HeapDumpOnOutOfMemoryError -Xmx2048m $APP > $CUR_DIR/console.txt 2>&1 &
   echo "Application started"
   echo $! > $CUR_DIR/pid.txt
}

CUR_DIR=$(_current_path)

echo "PATH: $CUR_DIR"
cd $CUR_DIR

if [ ! -f "$CUR_DIR/pid.txt" ]; then 
   #pid.txt is not exist, then the process is not exist!
   start_run
else 
   if
      ps ax | awk  '{print $1}' | grep -x $(cat $CUR_DIR/pid.txt)
   then
      echo "The process is running, please exec 'sh stop.sh' first!"
   else
      #echo "The process is running..."
      start_run
   fi
fi

2、停止jar运行

#!/bin/sh

_current_path() {
    SOURCE=${BASH_SOURCE[0]}
    DIR=$( dirname "$SOURCE" )
    while [ -h "$SOURCE" ]
    do
        SOURCE=$(readlink "$SOURCE")
        [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
        DIR=$( cd -P "$( dirname "$SOURCE"  )" && pwd )
    done
    DIR=$( cd -P "$( dirname "$SOURCE" )" && pwd )
    echo $DIR
}

CUR_DIR=$(_current_path)

echo "PATH: $CUR_DIR"
cd $CUR_DIR

CONSOLE_BAK_DIR="$CUR_DIR/console"

if [ ! -d "$CONSOLE_BAK_DIR" ]; then 
mkdir "$CONSOLE_BAK_DIR"
echo "MAKE DIR: $CONSOLE_BAK_DIR"
fi

CONSOLE_LOG_CUR="$CUR_DIR/console.txt"
CONSOLE_LOG_BAK="$CONSOLE_BAK_DIR/$(date '+%Y%m%d%H%M%S').txt"

kill `cat $CUR_DIR/pid.txt`
echo "Application exited"
if [ -f "$CONSOLE_LOG_CUR" ]; then 
mv "$CONSOLE_LOG_CUR" "$CONSOLE_LOG_BAK"
echo "Console log backup to: $CONSOLE_LOG_BAK"
fi

3、运行java项目

#!/bin/sh
APP="com.xxx.BestApp"
PROC_NAME="BestApp"

CLASS_PATH="./classes:./lib/*:./config:./*"

_current_path() {
    SOURCE=${BASH_SOURCE[0]}
    DIR=$( dirname "$SOURCE" )
    while [ -h "$SOURCE" ]
    do
        SOURCE=$(readlink "$SOURCE")
        [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
        DIR=$( cd -P "$( dirname "$SOURCE"  )" && pwd )
    done
    DIR=$( cd -P "$( dirname "$SOURCE" )" && pwd )
    echo $DIR
}

#启动程序脚本
start_run(){
   echo "Application starting"
   nohup java -Xms512m -Xmx2048m -cp $CLASS_PATH -XX:+HeapDumpOnOutOfMemoryError -Dproperty=$PROC_NAME -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9676  $APP  > $CUR_DIR/console.txt 2>&1 & 

   echo "Application started"
   echo $! > $CUR_DIR/pid.txt
}

CUR_DIR=$(_current_path)

echo "PATH: $CUR_DIR"
cd $CUR_DIR

if [ ! -f "$CUR_DIR/pid.txt" ]; then 
   #pid.txt不存在,说明进程未运行
   start_run
else 
   if
      ps -ax | awk  '{print $1}' | grep $(cat $CUR_DIR/pid.txt)
   then
      echo "进程已存在.如需重新启动,请先执行stop.sh!"
   else
         echo "进程启动..."
      start_run
   fi
fi

 

posted @ 2025-04-01 11:37  都是城市惹的祸  阅读(14)  评论(0)    收藏  举报