1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #!/bin/bash # 启动的方法参数([start|stop|restart|status]) PARAMETER=$1 #jar包路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到 JAR_NAME=$2 # 日志路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到 LOG_PATh=$3 # 如果输入格式不对,给出提示! tips() { echo "" echo "WARNING!!!......Tips, please use command: sh srart.sh [start|stop|restart|status]. For example: sh srart.sh start " echo "" exit 1 } # 启动方法 start() { # 重新获取一下pid,因为其它操作如stop会导致pid的状态更新 pid=`ps -ef |grep java | grep $JAR_NAME | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时执行 if [ -z $pid ]; then nohup java -jar $JAR_NAME > /dev/null 2>&1 & pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'` echo "" echo "Service ${JAR_NAME} is starting!pid=${pid}" echo "........................Here is the log.............................." echo "....................................................................." tail -f $LOG_PATh echo "........................Start successfully!........................." else echo "" echo "Service ${JAR_NAME} is already running,it's pid = ${pid}. If necessary, please use command: sh srart.sh restart." echo "" fi } # 停止方法 stop() { # 重新获取一下pid,因为其它操作如start会导致pid的状态更新 pid=`ps -ef | grep java | grep $JAR_NAME | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到 if [ -z $pid ];then echo "" echo "Service ${JAR_NAME} is not running! It's not necessary to stop it!" echo "" else kill -9 $pid echo "" echo "Service stop successfully!pid:${pid} which has been killed forcibly!" echo "" fi } # 输出运行状态方法 status() { # 重新获取一下pid,因为其它操作如stop、restart、start等会导致pid的状态更新 pid=`ps -ef | grep java | grep $JAR_NAME | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时执行。注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到 if [ -z $pid ];then echo "" echo "Service ${JAR_NAME} is not running!" echo "" else echo "" echo "Service ${JAR_NAME} is running. It's pid=${pid}" echo "" fi } # 重启方法 restart() { echo "" echo ".............................Restarting.............................." echo "....................................................................." # 重新获取一下pid,因为其它操作如start会导致pid的状态更新 pid=`ps -ef | grep java | grep $JAR_NAME | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到 if [ ! -z $pid ]; then kill -9 $pid fi start echo "....................Restart successfully!..........................." } # 根据输入参数执行对应方法,不输入则执行tips提示方法 case "${PARAMETER}" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) tips ;; esac
|