我爱java系列---【linux启动项目脚本】

linux启动项目脚本

三个文件:更改第一个文件,其余两个直接复制粘贴

1.project.sh文件

JAVA_HOME=$JAVA_HOME

JAVA_OPTS="-Xms5120m -Xmx5120m -XX:+UseParallelGC"

MAIN_CLASS=db-proxy             //改成自己的jar包上级目录名
JAR_PATH=db-proxy-0.0.1-SNAPSHOT.jar    //改成自己的jar包名

psid=0
execstop=0

checkpid() {
   pidfile="process.pid"

   if [ -f $pidfile ]; then
      psid=$(cat $pidfile)
   else
      psid=0
   fi
}

start() {
 
   checkpid
   cd ../ 
   if [ $psid -ne 0 ]; then
      echo "warn: $MAIN_CLASS already started! (pid=$psid)"
   else
      echo -n "Starting $MAIN_CLASS ..."
      nohup $JAVA_HOME/bin/java $JAVA_OPTS -jar $JAR_PATH  > logs/out.log & echo $! > bin/process.pid
      checkpid
      if [ $psid -ne 0 ]; then
         echo "(pid=$psid) [OK]"
      else
         echo "[Failed]"
      fi
   fi
}

stop() {
   checkpid
   if [ $execstop -ne 1 ]; then
       if [ $psid -ne 0 ]; then
           echo -n "Stopping $MAIN_CLASS ...(pid=$psid) "
           kill -9 $psid
           rm -rf process.pid
           if [ $? -eq 0 ]; then
               echo ""
               execstop=1
               stop
           else
               execstop=0
               echo "[Failed]"
           fi
        else
           echo "warn: $MAIN_CLASS is not running"
        fi
   else
       sleep 1
       echo -n "."
       if [ $psid -ne 0 ]; then
           stop
       else
           execstop=0
           echo ""
           echo "$MAIN_CLASS is stoped"
       fi
   fi
}

case "$1" in
   'start')
      start
      ;;
   'stop')
     stop
     ;;
  *)
     echo "Usage: $0 {start|stop}"
     exit 1
esac
exit 0

 

2.start.sh文件

G="$0"

while [ -h "$PRG" ] ; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

PRGDIR=`dirname "$PRG"`

exec "$PRGDIR"/project.sh start

 

3.shutdown.sh文件

G="$0"

while [ -h "$PRG" ] ; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

PRGDIR=`dirname "$PRG"`

exec "$PRGDIR"/project.sh stop

 

4.在jar包所在的目录中创建bin目录,并进入bin目录,执行:chmod 775 * 命令(*号可以是文件名/目录名,这里*代表所有)。

 

posted on 2020-02-13 22:27  少年攻城狮  阅读(277)  评论(0编辑  收藏  举报

导航