常见的linux上的服务重启脚本

手写linux上的重启脚本,先把提纲列下

1.检查进程是否存在

存在杀死

2.备份原来的包到指定目录

3. 拉取新包,我这边为了简便,没有从jenkins slave上拿

4.启动命令

5.检查是否进程起来了

 

#!/bin/bash

###path
JAVA=/usr/java/jdk1.8.0_211-amd64/bin/java
APP_HOME=/opt/88bugs
APPNAME=TestPlatform.jar
APP_PARAM="--spring.config.location=${APP_HOME}/application.properties"


####check wheather the port is running, if yes kill it
PID=`ps -ef | grep ${APP_HOME}/${APPNAME} | grep -v grep | awk '{print $2}'`
echo ${PID}

if test -z ${PID}
then 
	echo "进程没有启动!"

else 
	echo "will shutdown this process"
	kill -9 ${PID}
	sleep 2
fi


####backuop
mv ${APP_HOME}/${APPNAME} ${APP_HOME}/backup/${APPNAME}-date +%Y%m%d%H%M%S

####deploy the war package
cp -r /opt/${APPNAME}  ${APP_HOME}/${APPNAME}


##### start the process#
nohup ${JAVA} -jar ${APP_HOME}/${APPNAME} ${APP_PARAM}  > ${APP_HOME}/catalina.out 2>&1  &

afterPID=`ps -ef | grep ${APP_HOME}/${APPNAME} | grep -v grep | awk '{print $2}'`
echo ${afterPID}
sleep 10

if test -z ${afterPID}
then
	echo "部署失败"
else 
	echo "部署成功"
fi

 

若脚本希望既可以停止服务,又可以重启服务,

那么结构屡一下

配置环境,可供其他项目更改细节使用

获取输入,条件如果输入的是a,则执行a的方法;如果是b则执行b的方法体,什么都不是,即不是指定的指令,输出提示“您输入的指令没有对应的方法哟”

在上面的脚本上,更改些许即可

#!/bin/bash

#server 

JAVA=/usr/java/jdk1.8.0_211-amd64/bin/java
APP_HOME=/opt/88bugs
APP_NAME=TestPlatform.jar
APP_PARAM="--spring.config.location=${APP_HOME}/application.properties"

case $1 in
start)
PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')
if [ -z "$PID" ] ;then
        echo "start ${APP_NAME}"
        nohup ${JAVA} -jar ${APP_HOME}/${APP_NAME} ${APP_PARAM}  > ${APP_HOME}/catalina.out 2>&1  &
else
        echo "${APP_NAME} is running"
fi
;;
 
stop)
PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')
if [ -z "$PID" ] ;then
        echo "${APP_NAME} is not running"
else
        echo "stop ${APP_NAME}"
        kill -9 $PID
fi
;;
 
restart)
PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')
if [ -z "$PID" ] ;then
        echo "start ${APP_NAME}"
        nohup ${JAVA} -jar ${APP_HOME}/${APP_NAME} ${APP_PARAM}   > ${APP_HOME}/catalina.out 2>&1  &
else
        echo "stop ${APP_NAME}"
        kill -9 $PID
        echo "start ${APP_NAME}"
        nohup ${JAVA} -jar ${APP_HOME}/${APP_NAME} ${APP_PARAM}   > ${APP_HOME}/catalina.out 2>&1  &
fi
;;

backup)
PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')
if [ -z "$PID" ] ;then
        echo "${APP_NAME} is not running,will backup files"
	mv ${APP_HOME}/${APP_NAME} ${APP_HOME}/backup/${APP_NAME}-$(date +%Y%m%d%H%M%S)       
else
        echo "stop ${APP_NAME}"
        kill -9 $PID
        echo "will backup files..."
        mv ${APP_HOME}/${APP_NAME} ${APP_HOME}/backup/${APP_NAME}-$(date +%Y%m%d%H%M%S)
fi
;;
 
logs)
tail -f ${APP_HOME}/catalina.out
;;
 
status)
PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')
if [ -z "$PID" ] ;then
        echo "${APP_NAME} is not running"
else
        echo "${APP_NAME} is running, pid $PID"
fi
;;
 
*)
echo "您输入的指令没有对应的方法哟"
 
esac 

  

  

posted @ 2019-07-16 10:28  巴黎爱工作  阅读(3928)  评论(0编辑  收藏  举报