几种常见的启动脚本
1.spring boot (jar包形式)启停
#!/usr/bin/env bash APP_NAME=whale-uip-datasource-0.0.1-SNAPSHOT #JVM参数 JVM_OPTS="-DPGM_ID=$APP_NAME -Dname=$APP_NAME -Xmx1024M -XX:-UseGCOverheadLimit -Xmx512M -XX:PermSize=256M -XX:MaxPermSize=512M" JAR_FILE=$APP_NAME.jar start(){ nohup java -jar $JVM_OPTS $JAR_FILE --spring.config.location=./config/application-fjprod.yml & echo "starting completed,press CTRL+C to exit >>>>>" } stop(){ `ps -ef|grep DPGM_ID=$APP_NAME|grep -v grep|awk '{print "kill -9 "$2}'` } case $1 in start) start;; stop) stop;; *) echo "require start|stop" ;; esac
启动命令 ./***.sh start
停止命令 ./***.sh stop
2.项目类 main方式启停
启动脚本:
#!/bin/sh # JDK路径 # JAVA_HOME="/usr/java/jdk1.6.0_31" # Java程序主体所在的目录 APP_HOME=. #设置进程编码 APP_CODE=IOMC_HNYD_PROVIDER # Java程序日志所在的目录 APP_LOG=$APP_HOME/logs # JVM启动参数 # -server:一定要作为第一个参数,多个CPU时性能佳 # -Xloggc:记录GC日志,建议写成绝对路径,如此便可在任意目录下执行该shell脚本 JAVA_OPTS="-server -Xms64m -Xmx64m -Xloggc:$APP_LOG/gc.log" # Java主程序,也就是main(String[] args)方法类 APP_MAIN=com.ztesoft.iomc.web.server.Bootstrap # classpath参数,包括指定lib目录下的所有jar OLDCLASSPATH=${CLASSPATH} CLASSPATH=. for thirdjar in $APP_HOME/lib/*.jar; do CLASSPATH=$CLASSPATH:$thirdjar done if [ "$OLDCLASSPATH" != "" ]; then CLASSPATH=${CLASSPATH}:${OLDCLASSPATH} fi echo $CLASSPATH # 初始化全局变量,用于标识交易前置系统的PID(0表示未启动) appPID=0 # 获取Java应用的PID # ------------------------------------------------------------------------------------------------------ # 说明:通过JDK自带的jps命令,联合Linux中的grep命令,可以准确查找到Java应用的PID # [jps -l]表示显示Java主程序的完整包路径 # awk命令可以分割出PID($1部分)及Java主程序名称($2部分) # 例子:[$JAVA_HOME/bin/jps -l | grep $APP_MAIN]命令执行,会看到[5775 com.cucpay.tradeportal.MainApp] # 另外:这个命令也可以取到程序的PID-->[ps aux|grep java|grep $APP_MAIN|grep -v grep|awk '{print $2}'] # ------------------------------------------------------------------------------------------------------ getAppPID(){ #javaps=`$JAVA_HOME/bin/jps -l | grep $APP_CODE` appps=`ps -ef|grep java|grep $APP_CODE` if [ -n "$appps" ]; then appPID=`echo $appps | awk '{print $2}'` else appPID=0 fi } # 启动Java应用程序 # ------------------------------------------------------------------------------------------------------ # 1、调用 getAppPID() 函数,刷新$appPID全局变量 # 2、若程序已经启动($appPID不等于0),则提示程序已启动 # 3、若程序未被启动,则执行启动命令 # 4、启动命令执行后,再次调用getAppPID()函数 # 5、若步骤4执行后,程序的PID不等于0,则打印Success,反之打印Failed # 注意:[echo -n]表示打印字符后不换行 # 注意:[nohup command > /path/nohup.log &]是将作业输出到nohup.log,否则它会输出到该脚本目录下的nohup.out中 # ------------------------------------------------------------------------------------------------------ start(){ getAppPID echo "===============================================================================================" if [ $appPID -ne 0 ]; then echo "$APP_CODE already started(PID=$appPID)" echo "===============================================================================================" else echo -n "Starting $APP_CODE" #nohup $JAVA_HOME/bin/java $JAVA_OPTS -classpath $CLASSPATH $APP_MAIN > $APP_LOG/nohup.log & nohup $JAVA_HOME/bin/java -DPGM_ID=$APP_CODE -DAPP_NAME=$APP_CODE $JAVA_OPTS -cp .:./bin:${CLASSPATH} -Dfile.encoding=utf-8 $APP_MAIN > $APP_LOG/nohup.log & getAppPID if [ $appPID -ne 0 ]; then echo "(PID=$appPID)...[Success]" echo "===============================================================================================" else echo "[Failed]" echo "===============================================================================================" fi fi } # 调用启动命令 start tail -f ./logs/nohup.log
启动命令 ./***.sh start
停止脚本:
#!/bin/sh #APP_MAIN=com.ztesoft.iomc.web.server.WebBootstrap APP_CODE=IOMC_HNYD_PROVIDER appPID=0 getAppPID(){ #javaps=`$JAVA_HOME/bin/jps -l | grep $APP_CODE` appps=`ps -ef|grep java|grep $APP_CODE` if [ -n "$appps" ]; then appPID=`echo $appps | awk '{print $2}'` else appPID=0 fi } # 停止Java应用程序 # ------------------------------------------------------------------------------------------------------ # 1、调用getAppPID()函数,刷新$appPID全局变量 # 2、若程序已经启动($appPID不等于0),则开始执行停止程序操作,否则提示程序未运行 # 3、使用[kill -9 PID]命令强制杀掉进程 # 4、使用[$?]获取上一句命令的返回值,若其为0,表示程序已停止运行,则打印Success,反之打印Failed # 5、为防止Java程序被启动多次,这里增加了反复检查程序进程的功能,通过递归调用shutdown()函数的方式,反复kill # 注意:Shell编程中,[$?]表示上一句命令或者上一个函数的返回值 # ------------------------------------------------------------------------------------------------------ stop(){ getAppPID echo "===============================================================================================" if [ $appPID -ne 0 ]; then echo -n "Stopping $APP_CODE(PID=$appPID)..." kill -9 $appPID if [ $? -eq 0 ]; then echo "[Success]" echo "===============================================================================================" else echo "[Failed]" echo "===============================================================================================" fi getAppPID if [ $appPID -ne 0 ]; then shutdown fi else echo "$APP_CODE is not running" echo "===============================================================================================" fi } stop
停止命令 ./***.sh stop
查看状态脚本:
#!/bin/sh #APP_MAIN=com.ztesoft.iomc.web.server.WebBootstrap APP_CODE=IOMC_HNYD_PROVIDER appPID=0 getAppPID(){ # javaps=`$JAVA_HOME/bin/jps -l | grep $APP_CODE` appps=`ps -ef|grep java|grep $APP_CODE` if [ -n "$appps" ]; then appPID=`echo $appps | awk '{print $2}'` else appPID=0 fi } status(){ getAppPID echo "===============================================================================================" if [ $appPID -ne 0 ]; then echo "$APP_CODE is running(PID=$appPID)" echo "===============================================================================================" else echo "$APP_CODE is not running" echo "===============================================================================================" fi } status
查看命令 ./***.sh status
3.指定jdk版本,启动spring boot
启动脚本
nohup /app/weblogic/bea923/jdk1.8.0_11/bin/java -server -Xmx2048m -Xms2048m -Xss256k -jar sim-outyd-server-0.0.1-SNAPSHOT.jar &
停止脚本
PID=$(ps -ef | grep sim-outyd-server-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{ print $2 }') if [ -z "$PID" ] then echo Application is already stopped else echo kill $PID kill $PID fi
重启脚本
echo stop application source stop.sh echo start application source start.sh sleep 3 tail -f logs/msg.log
调用 直接使用./脚本名.sh
4.spring cloud完整版启动脚本
下面脚本包含start、stop、status
#!/usr/bin/env bash APP_NAME=gateway-0.0.1-SNAPSHOT #JVM参数 #JVM_OPTS="-DPGM_ID=$APP_NAME -Dname=$APP_NAME -Xmx1024M -XX:-UseGCOverheadLimit -Xmx512M -XX:PermSize=256M -XX:MaxPermSize=512M" JVM_OPTS="-DPGM_ID=$APP_NAME -Dname=$APP_NAME" JAR_FILE=$APP_NAME.jar start(){ nohup java -jar $JVM_OPTS $JAR_FILE >/dev/null 2>&1& P_ID=`ps -ef | grep -w $JAR_FILE | grep -v "grep" | awk '{print $2}'` echo "$APP_NAME is running(PID=$P_ID)" } stop(){ P_ID=`ps -ef | grep -w $JAR_FILE | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===$APP_NAME process not exists or stop success" else kill -9 $P_ID echo "$APP_NAME killed success" fi } status(){ P_ID=`ps -ef | grep -w $JAR_FILE | grep -v "grep" | awk '{print $2}'` if [ "$P_ID" == "" ]; then echo "===$APP_NAME is not running" else echo "$APP_NAME is running(PID=$P_ID)" fi } case $1 in start) start;; stop) stop;; status) status;; *) echo "require start|stop" ;; esac exit 0
在部署上面脚本后,可以再编写一个统一shell脚本,一键启动一系列的sh脚本。
#!/bin/sh case "$1" in start) ## 启动gateway echo "--------gateway 开始启动--------------" cd /home/app/gateway ./gateway.sh start echo "--------gateway 启动成功--------------" ;; stop) echo "--------gateway 开始停止--------------" cd /home/app/gateway ./gateway.sh stop echo "--------gateway 停止成功--------------" ;; status) cd /home/app/gateway ./gateway.sh status ;; restart) $0 stop sleep 2 $0 start echo "===restartAll success===" ;; esac exit 0
5.同时启停多个docker容器
下面脚本包含start、stop、status
#!/bin/bash containerIDs="5804f51e186 07252160a61 fae55fc3167" statusLived="live" statusdead="Dead" notExistContainer="None" retryCount=1 function GetContainerStatus(){ containerExist=$(sudo docker ps -a | grep -i $1 | wc -l ) if [ ${containerExist} -gt 0 ] then pid=$(sudo docker stats --format "{{.PIDs}}" --no-stream $1 ) if [ "${pid}" != "0" ] then echo "${statusLived}" else echo "${statusdead}" fi else echo "${notExistContainer}" fi } function StartContainer(){ sudo docker restart $1 } function StopContainer(){ sudo docker stop $1 } start(){ for containerID in ${containerIDs} do for((i=1;i<=${retryCount};i++)) do status=$(GetContainerStatus ${containerID} ) echo "Container ${containerID} status is ${status}" if [ "${status}" == ${statusLived} ] then echo "Container ${containerID} already running" break fi if [ "${status}" == ${notExistContainer} ] then echo "Container ${containerID} not existed" break fi if [ "${status}" == ${statusdead} ] then echo "Container ${containerID} stopped ,start container" StartContainer ${containerID} verifyStatus=$(GetContainerStatus ${containerID} ) if [ "${verifyStatus}" == ${statusLived} ] then echo "start container ${containerID} success " break else echo "${i} retry start container" StartContainer ${containerID} fi fi done done } stop(){ for containerID in ${containerIDs} do for ((i=1;i<=${retryCount};i++)) do status=$(GetContainerStatus ${containerID} ) echo "Container ${containerID} status is ${status}" if [ "${status}" == ${statusdead} ] then echo "Container ${containerID} already stopped" break fi if [ "${status}" == ${notExistContainer} ] then echo "Container ${containerID} not existed" break fi if [ "${status}" == ${statusLived} ] then echo "Container ${containerID} is lived ,stop container" StopContainer ${containerID} verifyStatus=$(GetContainerStatus ${containerID} ) if [ "${verifyStatus}" == ${statusdead} ] then echo "stop container ${containerID} success " break else echo "${i} retry stop container" StopContainer ${containerID} fi fi done done } status(){ for containerID in ${containerIDs} do for ((i=1;i<=${retryCount};i++)) do status=$(GetContainerStatus ${containerID} ) echo "Container ${containerID} status is ${status}" done done } case $1 in start) start;; stop) stop;; status) status;; *) echo "require start|stop" ;; esac exit 0