shell 啟動 java jar 檔案

不設限發表於2016-09-30

1.shell 檔案: service-restart.sh

#!/bin/sh
#java 環境變數
source /etc/profile
export JAVA_HOME=/home/tools/jdk1.7.0_67
export JRE_HOME=$JAVA_HOME/jre

#判斷傳入的引數數量是不是夠
if [ $# != 2 ] ; then 
echo "USAGE: $0 SERVICE_DIR SERVICE_NAME_PRE" 
echo " e.g.: $0 /opt/ync-goods ync-goods" 
exit 1; 
fi 
SERVICE_DIR=$1
SERVICE_NAME_PRE=$2

echo "SERVICE_DIR" $SERVICE_DIR
echo "SERVICE_NAME_PRE" $SERVICE_NAME_PRE

cd $SERVICE_DIR

 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 名稱
    JAR_NAME=`ls -rt *.jar|grep -v 'sources'|tail -1`

    echo "START JAR_NAME "$JAR_NAME
    $JRE_HOME/bin/java -Xms256m -Xmx512m -jar $JAR_NAME > $SERVICE_NAME_PRE.log  2>&1 &
exit

2.使用示例:
2.1.第一個引數是可執行 jar 檔案所在目錄
2.2.第二個引數是可執行 jar 檔名稱

/opt/service-restart.sh /opt/test test

相關文章