用於管理應用程式得shell指令碼

handx發表於2018-09-30

用於管理應用程式得shell指令碼

#!/bin/sh

APP_NAME="應用名稱"
JAVA_OPTS="-Xms256m -Xmx256m -Dfile.encoding=UTF-8"
psid=0

checkpid() {
   javaps=$(pgrep -f "APP_NAME")

   if [ -n "$javaps" ]; then
      psid=$javaps
   else
      psid=0
   fi
}

start() {
   checkpid

   if [ $psid -ne 0 ]; then
      echo "================================"
      echo "warn: $APP_NAME already started! (pid=$psid)"
      echo "================================"
   else
      echo "Starting $APP_NAME ..."
      nohup java $JAVA_OPTS -jar $APP_NAME.jar >/dev/null 2>&1 &

      checkpid
      if [ $psid -ne 0 ]; then
         echo "(pid=$psid) [OK]"
      else
         echo "[Failed]"
      fi
   fi
}

stop() {
   checkpid

   if [ $psid -ne 0 ]; then
      echo -n "Stopping $APP_NAME ...(pid=$psid) "
      kill $psid
      if [ $? -eq 0 ]; then
         echo "[OK]"
      else
         echo "[Failed]"
      fi

      checkpid
      if [ $psid -ne 0 ]; then
         stop
      fi
   else
      echo "================================"
      echo "warn: $APP_NAME is not running"
      echo "================================"
   fi
}

status() {
   checkpid
   if [ $psid -ne 0 ];  then
      echo "$APP_NAME is running! (pid=$psid)"
   else
      echo "$APP_NAME is not running"
   fi
}

showlog() {
   tail -f logs/log.log
}

case "$1" in
   'start')
      start
      ;;
   'stop')
     stop
     ;;
   'restart')
     stop
     start
     ;;
   'status')
     status
     ;;
   'log')
     showlog
     ;;
  *)
     echo "Usage: $0 {start|stop|restart|status|info}"
     exit 1
esac
exit 0
複製程式碼

相關文章