1.示例脚本
#r包路径,加不加引号。 注:等号两侧 不能 有空格,否则会提示command找不到#!/bin/bashJAR_ALLNAME="/app/energyMonitor/yurgqg.jar"JAR_NAME="yurgqg.jar"#JAR_NAME2=”admin-service/admin-service.jar"# 日志路径,加不加引号。 注:等号两侧 不能 有空格,否则会提示command找不到LOG_PATh=yurgqg.log# 如果输入格式错误,给出提示!tips() {echo ""echo "WARNING!!!...Tips, please use command: sh auto_deploy.sh [start|stop|restart|status]. For example: sh auto_deploy.sh start "echo ""exit 1}# start()启动方法 { # 再次获得pid,因为stop等其他操作会导致pid状态更新pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'` # -z 如果$pid空时执行if,则表示 [ -z $pid ]; then nohup java -jar $JAR_ALLNAME >> $LOG_PATh 2>&1 echo "启动${JAR_NAME}"fi}# stop()停止方法 {# 再次获得pid,因为start等其他操作会导致pid状态更新pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'` # -z 如果$pid是空时执行的话。 注:每个命令和变量之间必须前后加空间,否则会提示command找不到iff [ -z $pid ]; thenecho "" echo "Service ${JAR_NAME} is not running! 注:每个命令和变量之间必须前后加空间,否则会提示command找不到iff [ -z $pid ]; thenecho "" echo "Service ${JAR_NAME} is not running! It's not necessary to stop it!"echo ""elsekill -9 $pidecho ""echo "Service stop successfully!pid:${pid} which has been killed forcibly!"echo ""fi}# status()输出运行状态方法 { # 再次获得pid,因为其他操作如stopp等、restart、start等会导致pid状态更新pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'` # -z 如果$pid是空时执行的话。注:每个命令和变量之间必须前后加空间,否则会提示command找不到iff [ -z $pid ];thenecho "" echo "Service ${JAR_NAME} is not running!"echo ""elseecho "" echo "Service ${JAR_NAME} is running. It's pid=${pid}"echo ""fi}# restart()重启方法 {echo ""echo "...Restarting..."echo "..."# 再次获得pid,因为start等其他操作会导致pid状态更新pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'` # -z 如果$pid是空时执行的话。 注:每个命令和变量之间必须前后加空间,否则会提示command找不到iff [ ! 注:每个命令和变量之间必须前后加空间,否则会提示command找不到iff [ ! -z $pid ]; thenkill -9 $pidfistartecho "...Restart successfully!..."}# 按输入参数执行相应的方法,不输入,执行tips提示方法case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) tips ;;esac
2.启功命令
/bin/sh /app/energyMonitor/yu.sh start
示例
2.检查程序状态命令
/bin/sh /app/energyMonitor/yu.sh status
示例
插图描述3.程序终止命令
/bin/sh /app/energyMonitor/yu.sh stop
示例