1.開機檔案: service-restart.sh
#!/bin/sh## java envsource /etc/profileexport JAVA_HOME=/usr/local/jdk1.7.0_75export JRE_HOME=$JAVA_HOME/jreif [ $# != 3 ] ; then echo "USAGE: $0 SERVICE_DIR SERVICE_NAME_PRE DISCONF_ENV" echo " e.g.: $0 /opt/dubbo-test dubbo-goods rd" exit 1; fi SERVICE_DIR=$1SERVICE_NAME_PRE=$2#設定 disconf 資訊DISCONF_ENV=$3DISCONF_HOST=192.168.0.11:8080DISCONF_APP=dubboDISCONF_VERSION=1.0.0echo "SERVICE_DIR" $SERVICE_DIRecho "SERVICE_NAME_PRE" $SERVICE_NAME_PREecho "DISCONF_ENV" $DISCONF_ENVecho "DISCONF_HOST" $DISCONF_HOSTecho "DISCONF_APP" $DISCONF_APPecho "DISCONF_VERSION" $DISCONF_VERSIONcd $SERVICE_DIR# P_ID=`ps -ef | grep -w "$SERVICE_NAME_PRE" | grep -v "grep" | awk '{print $2}'` P_ID=`jps -ml|grep "$SERVICE_NAME_PRE"|awk '{print $1}'` if [ "$P_ID" == "" ]; then echo "=== $SERVICE_NAME process not exists or stop success" else echo "=== begin kill $SERVICE_NAME process, pid is:$P_ID" kill -9 $P_ID fi sleep 3 JAR_NAME=`ls -rt *.jar|grep -v 'sources'|tail -1` echo "START JAR_NAME "$JAR_NAME nohup $JRE_HOME/bin/java -Xms256m -Xmx512m -jar -Ddisconf.env=$DISCONF_ENV -Ddisconf.conf_server_host=$DISCONF_HOST -Ddisconf.app=$DISCONF_APP -Ddisconf.version=$DISCONF_VERSION -Ddisconf.user_define_download_dir=.classes -Ddisconf.enable.remote.conf=true $JAR_NAME >$SERVICE_NAME_PRE.log 2>&1 & ls *.jar|grep -v $JAR_NAME|xargs rm -rfexit 0
2.例子:
2.1.第一個參數是可執行 jar 檔案所在目錄
2.2.第二個參數是可執行 jar 檔案名稱
2.3.第三個參數是環境名稱
/opt/service-restart.sh /opt/test-service test-service rd