linux shell脚本调用java main方法 代码传值

时间:2022-01-23 15:57:56
#!/bin/bash
#description: ljdjService
export PRG_HOME=/ainmc/work/toptea/dataTransfer
PRG_KEYWORD=projectName
PRG=$PRG_HOME/server.sh
JAVA_HOME=/aa/bb/jdk1.8.0_111  #jdk安装位置
LOGCFG="-Dlogback.configurationFile=$PRG_HOME/conf/logback.xml"
FILE_ENCODING="-Dfile.encoding=utf-8"
jvmOpt="-Xms2048m -Xmx2048m -XX:PermSize=256M -XX:MaxPermSize=512m"
PRG_RUN_USER=nmcuser
pidFile=$PRG_HOME/server.pid
MAIN_CLASS=com.ai.vv.aa.Main # main方法的路径
jarDir=$PRG_HOME/lib
transferType="$2"
cd $PRG_HOME
function start(){
        
        echo "will start $PRG_KEYWORD $transferType"
        export CLASSPATH=.
        for jar in `ls $PRG_HOME/lib/*.jar`
        do
                CLASSPATH=$CLASSPATH:$jar
        done
       # nohup "$JAVA_HOME"/bin/java -Djobname="$PRG_KEYWORD"  -cp "$CLASSPATH" $jvmOpt $LOGCFG $FILE_ENCODING $MAIN_CLASS add "$transferType" >/dev/null 2>&1&
        "$JAVA_HOME"/bin/java -Djobname="$PRG_KEYWORD"  -cp "$CLASSPATH" $jvmOpt $LOGCFG $FILE_ENCODING $MAIN_CLASS  add "$transferType"
        for(( i=1;i<=10;i++));do
                if [ -e $pidFile ]; then
                        echo "$PRG_KEYWORD is started,pid=`cat $pidFile`"
                        break
                fi
                sleep 2
        done
}
function delete(){
        
        echo "will start $PRG_KEYWORD"
        export CLASSPATH=.
        for jar in `ls $PRG_HOME/lib/*.jar`
        do
                CLASSPATH=$CLASSPATH:$jar
        done
       # nohup "$JAVA_HOME"/bin/java -Djobname="$PRG_KEYWORD"    -cp "$CLASSPATH" $jvmOpt $LOGCFG $FILE_ENCODING $MAIN_CLASS delete "$transferType">/dev/null 2>&1&
        "$JAVA_HOME"/bin/java -Djobname="$PRG_KEYWORD" -cp "$CLASSPATH" $jvmOpt $LOGCFG $FILE_ENCODING $MAIN_CLASS  delete "$transferType"
        for(( i=1;i<=10;i++));do
                if [ -e $pidFile ]; then
                        echo "$PRG_KEYWORD is started,pid=`cat $pidFile`"
                        break
                fi
                sleep 2
        done
}
psid=0
checkpid() {
   javaps=`$JAVA_HOME/bin/jps -l | grep $PRG_KEYWORD`
 
   if [ -n "$javaps" ]; then
      psid=`echo $javaps | awk '{print $1}'`
   else
      psid=0
   fi
}

    stop() {  
       checkpid  
      
       if [ $psid -ne 0 ]; then  
          echo -n "Stopping $PRG_KEYWORD ...(pid=$psid) "  
          kill -9 $psid
          if [ $? -eq 0 ]; then  
             echo "[OK]"  
          else  
             echo "[Failed]"  
          fi  
      
          checkpid  
          if [ $psid -ne 0 ]; then  
             stop  
          fi  
       else  
          echo "================================"  
          
          echo "================================"  
       fi  
    }  
case "$1" in
start)
        if [ -e $pidFile ]; then
                pid=`cat $pidFile`
                tpid=`ps -ef | grep java | grep -v grep | grep jobname=$PRG_KEYWORD | grep -c "$PRG_RUN_USER "`
                if [ $tpid -lt 1 ]; then
                        start
                else
                        echo "$PRG_KEYWORD already started!! pid="$pid
                fi
        else
                tpid=`ps -ef | grep java | grep -v grep | grep jobname=$PRG_KEYWORD | grep -c "$PRG_RUN_USER "`
                pid=`ps -ef | grep java | grep -v grep | grep jobname=$PRG_KEYWORD | grep "$PRG_RUN_USER " | awk '{print $2}'`
                if [ $tpid -lt 1 ]; then
                        start
                else
                        echo "$PRG_KEYWORD already started!! pid="$pid
                        echo "$pid">server.pid
                fi
        fi
;;

del)
        if [ -e $pidFile ]; then
                pid=`cat $pidFile`
                tpid=`ps -ef | grep java | grep -v grep | grep jobname=$PRG_KEYWORD | grep -c "$PRG_RUN_USER "`
                if [ $tpid -lt 1 ]; then
                        delete
                else
                        echo "$PRG_KEYWORD already started!! pid="$pid
                fi
        else
                tpid=`ps -ef | grep java | grep -v grep | grep jobname=$PRG_KEYWORD | grep -c "$PRG_RUN_USER "`
                pid=`ps -ef | grep java | grep -v grep | grep jobname=$PRG_KEYWORD | grep "$PRG_RUN_USER " | awk '{print $2}'`
                if [ $tpid -lt 1 ]; then
                        delete
                else
                        echo "$PRG_KEYWORD already started!! pid="$pid
                        echo "$pid">server.pid
                fi
        fi
;;
stop)
stop;;
*)
        echo "Usage: server.sh (start|del|stop 工单类型英文关键字)"
esac