linux中java 裏面啓動 重啓 停止jar 的 shell

#kconfig: - 95 15   
# description: 抓取程序  start/stop/status script

#JDk 所在的路徑 
export JAVA_HOME=/home/java/jdk1.8.0_60

#Add Java binary files to PATH  
export PATH=$JAVA_HOME/bin:$PATH  

#需要啓動的Java主程序(main方法類)  
PROGRAM_NAME="com.hnmzhc.daemon.Main"

#USAGE is the message if this script is called without any options  
USAGE="Usage: $0 {start,stop,status,restart}"  

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop  
SHUTDOWN_WAIT=20  
#獲取執行id
tomcat_pid() {  
        echo `ps -ef | grep $PROGRAM_NAME | grep -v grep | tr -s " "|cut -d" " -f2  `
}  

##啓動
start() {  
  pid=$(tomcat_pid)  
  if [ -n "$pid" ];then  
    echo -e "spider is already running (pid: $pid)"  
  else  
    echo -e "Starting spider"  
    nohup java -Dfile.encoding=UTF-8 -classpath "./yjtx-daemon-1.0-SNAPSHOT.jar:." com.hnmzhc.daemon.Main   > /dev/null 2>&1  &
    status  
  fi  
  return 0  
}  
##############
#狀態
#######
status(){  
  pid=$(tomcat_pid)  
  if [ -n "$pid" ];then  
    echo -e "spider is running with pid: $pid"  
  else  
    echo -e "spider  is not running"  
  fi  
}  
##停止
stop() {  
  pid=$(tomcat_pid)  
  if [ -n "$pid" ];then  
    echo -e "Stoping spider"  
    #  shutdown.sh  

    #let kwait=$SHUTDOWN_WAIT  count=0;  
    #until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]  
    #do  
    #  echo -n -e "waiting for processes to exit";  
    #  sleep 1  
    #  let count=$count+1;  
    #done  

    if [ $count -gt $kwait ];then  
      echo -n -e "killing processes which didn't stop after $SHUTDOWN_WAIT seconds"  
      kill -9 $pid  
    fi  
  else  
    echo -e "spider  is not running"  
  fi  

  return 0  
}  

user_exists(){  
  if id -u $1 >/dev/null 2>&1; then  
    echo "1"  
  else  
    echo "0"  
  fi  
}  

case $1 in  
        start)  
          start  
        ;;  

        stop)    
          stop  
        ;;  

        restart)  
          stop  
          start  
        ;;  

        status)  
      status  
        ;;  

        *)  
      echo -e $USAGE  
        ;;  
esac      
exit 0 

上面的shell 和我們的執行jar放在一個文件夾下面居可以了

發佈了48 篇原創文章 · 獲贊 39 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章