Java项目通过启动jar启动项目脚本

贴出一个项目启动脚本,内容如下:

#!/bin/bash
# Steps:
# Removing /app/sit/<app_name>
# unzip /app/sit/.candidates/<app_name>.zip to /app/sit/<app_name>/app
# cd to /app/sit/<app_name>/app, and run java -jar ${JAVA_OPTS} xxx.jar
#
# logs are stored in /app/sit/<app_name>/logs
# If you want to overwrite config files in the jar, move it to /app/sit/<app_name>/conf
#
# /app/sit
#     |── .candidates
#     │   └──xxx.zip (Jenkins build with `deploy=true` will place zips here)
#     └── <app_name>
#         ├── app
#         │   ├── <app_name>-<version>.jar
#         │   └── lib
#         │       ├── activemq-all-5.11.3.jar
#         │       ├── ...
#         │       └── utility-cache-redis-0.0.1-SNAPSHOT.jar
#         ├── conf
#         │   └── env-xxx.properties (optional)
#         └── logs
#             └── biz.log
#######################################################################################

################  variable starts  ##############

JAVA_OPTS=${JAVA_OPTS:-"-Xms256m -Xmx256m"}
#JAVA_OPTS="-server -Xms8G -Xmx8G -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
#JAVA_OPTS="-server -Xms512m -Xmx512m -XX:SurvivorRatio=2 -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly -XX:+ScavengeBeforeFullGC -XX:+CMSScavengeBeforeRemark -XX:CMSInitiatingOccupancyFraction=65 -XX:+ParallelRefProcEnabled -verbose:gc -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+PrintTenuringDistribution -Xloggc:../logs/gc-`date +%F_%H-%M-%S`.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=50M -Dsun.net.inetaddr.ttl=30 -Djava.security.egd=file:/dev/./urandom -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs/dump-`date +%F_%H-%M-%S`.hprof"

################  variable stops  ###############

work_dir=${PWD}/app
pidfile=${PWD}/pid 
app_name=`pwd`

############## functions ###############
start() {
  >> app/nohup.out
  cd ${work_dir}
  echo "starting..."
  nohup $JAVA_HOME/bin/java ${JAVA_OPTS} -jar *.jar  &
#  nohup java ${JAVA_OPTS} -jar *.jar >/dev/null 2>&1 &
  echo $! > ${pidfile}
  echo "started."
}

stop() {
  if is_running; then
    kill  ${pid}
    echo "${app_name} stopped successfully."
  else
    echo "${app_name} is NOT running"
    rm -f ${pidfile}
  fi
}

status() {
  if is_running; then
    echo "${app_name} is running..."
  else
    echo "${app_name} is NOT running"
  fi
}

is_running() {
  pid=`cat ${pidfile}`
  if [ -e "/proc/${pid}" ] && [ "java" == "`cat /proc/${pid}/comm`" ]; then
    return 0
  else
    rm -f ${pidfile}
    return 1
  fi
}

############## functions calls ###############
case $1 in
start)
  if is_running; then
    echo "${app_name} is already running."
  else
    start
  fi
  ;;
stop)
  if [ -e "${pidfile}" ];then
    stop
    exit 1
  else
    echo "${app_name} is NOT running"
  fi
  ;;
status)
  status
  ;;
restart)
  stop
  sleep 1
  start
  ;;
deploy)
  stop
  sleep 1
  deploy_zip
  sleep 1
  start
  ;;
*)
  echo "./bootstrap.sh (start|stop|restart|deploy|status)"
esac

之后通过./bootstrap.sh (start|stop|restart|deploy|status) 脚本启动服务了

猜你喜欢

转载自blog.csdn.net/weixin_39352976/article/details/107711799