Linux 啓動停止重啓 springboot jar包 腳本

#!/bin/bash
#這裏可替換爲你自己的執行程序,其他代碼無需更改
APP_NAME=common.jar
 
#使用說明,用來提示輸入參數
usage() {
    echo "Usage: sh 腳本名.sh [start|stop|restart|status]"
    exit 1
}
 
#檢查程序是否在運行
is_exist(){
  pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
  #如果不存在返回1,存在返回0     
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}
 
#啓動方法
start(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is already running. pid=${pid} ."
  else
    nohup java -jar $APP_NAME > /dev/null 2>&1 &
  fi
}
 
#停止方法
stop(){
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
  else
    echo "${APP_NAME} is not running"
  fi  
}
 
#輸出運行狀態
status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is running. Pid is ${pid}"
  else
    echo "${APP_NAME} is NOT running."
  fi
}
 
#重啓
restart(){
  stop
  start
}
 
#根據輸入參數,選擇執行對應方法,不輸入則執行使用說明
case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    restart
    ;;
  *)
    usage
    ;;
esac

註釋:

一、用途:nohup表示永久運行。&表示後臺運行

在應用Unix/Linux時,我們一般想讓某個程序在後臺運行,nohup ./start-mysql.sh &

該命令的一般形式爲:nohup command &

在缺省情況下該作業的所有輸出都被重定向到一個名爲nohup.out的文件中,除非另外指定了輸出文件:

nohup command > myout.file 2>&1 &

在上面的例子中,輸出被重定向到myout.file文件中。

二、>/dev/null 2>&1

/dev/null 代表空設備文件,也就是不輸出任何信息到終端,說白了就是不顯示任何信息。
> 代表重定向到哪裏
1 表示stdout標準輸出,系統默認值是1,所以">/dev/null"等同於"1>/dev/null"
2 表示stderr標準錯誤
& 表示等同於的意思,2>&1,表示2的輸出重定向等同於1
nohup ./mqnamesrv >/home/cxb/mqnamesrv.out 2>&1 & 
即標準輸出到mqnamesrv.out中,接着,標準錯誤輸出重定向等同於標準輸出,輸出到同一文件中。

三、使用 jobs 查看任務。

使用 fg %n 關閉。

四、sh xxx.sh與./xxx.sh區別

sh xxx.sh是用sh 執行start.sh,start.sh可以沒有執行標誌,可以不用加./,可以不用在腳本第一行寫上#!/bin/sh
./start.sh是調用腳本第一行制定的shell去解釋執行,缺省爲sh,就是bash

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章