Java應用啟動指令碼
阿新 • • 發佈:2020-09-11
#!/bin/bash # # author:liusha # description: JAVA APP start|stop|restart|status. ENV=dev APP_HOME=`pwd` APP_NAME=easyJob RUNNING_USER=root ADATE=`date +%Y%m%d%H%M%S` dirname $0|grep "^/" >/dev/null if [ $? -eq 0 ]; then APP_HOME=`dirname $0` else dirname $0|grep "^\." >/dev/null: retval=$? if[ $retval -eq 0 ]; then APP_HOME=`dirname $0|sed "s#^.#$APP_HOME#"` else APP_HOME=`dirname $0|sed "s#^#$APP_HOME/#"` fi fi if [ ! -d "$APP_HOME/logs" ]; then mkdir $APP_HOME/logs fi #實時日誌 LOG_PATH=$APP_HOME/logs/$APP_NAME.out #GC日誌資訊 GC_LOG_PATH=$APP_HOME/logs/gc-$APP_NAME-$ADATE.log #JMX監控引數 JMX="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1091 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false" #JVM啟動引數 JVM_OPTS="-Dname=$APP_NAME -Djeesuite.configcenter.profile=$ENV -Duser.timezone=Asia/Shanghai -Xms512M -Xmx512M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -Xloggc:$GC_LOG_PATH -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC" JAR_FILE=$APP_NAME.jar pid=0 #獲取程序號 getPid() { pid=`ps -ef |grep $JAR_FILE |grep -v grep |awk '{print $2}'` } #啟動服務 start() { getPid if [ ! -n "$pid" ]; then JAVA_CMD="nohup java -jar $JVM_OPTS $JAR_FILE >> $LOG_PATH 2>&1 &" echo "---------------------------------" su $RUNNING_USER -c "$JAVA_CMD" echo "啟動完成,按CTRL+C退出日誌介面即可>>>>>" echo "---------------------------------" sleep 2s tail -f $LOG_PATH else echo "$APP_NAME is runing PID: $pid" fi } #停止服務 stop() { getPid if [ ! -n "$pid" ]; then echo "$APP_NAME not runing" else echo "$APP_NAME stop..." kill -9 $pid fi } #重啟服務 restart() { stop sleep 1s start } #服務狀態 status() { getPid if [ ! -n "$pid" ]; then echo "$APP_NAME not runing" else echo "$APP_NAME runing PID: $pid" fi } case $1 in start) start ;; stop) stop ;; restart) restart ;; status) status ;; *) echo "$0: Usage: $0 {start|stop|restart|status}" exit 1 ;; esac