杀死服务进程并重新启动,同时监听日志打印

工作中部署新jar包后,需要重启jar包服务,每次需要4步操作:

  1. ps命令找到进程pid
  2. kill命令杀死进程
  3. jar命令启动jar包
  4. tail命令查看启动日志

脚本stop2start.sh

#!/bin/sh
sp_pid=`ps -ef | grep management | grep -v grep | awk '{print $2}'`
if [ -z "$sp_pid" ];
then
 echo "[ not find sp-tomcat pid ]"
else
 echo "find result: $sp_pid "
 kill -9 $sp_pid
fi
nohup java -jar management-0.0.1-SNAPSHOT.jar &
tail -f nohup.out

grep 后边修改为要关闭的进行名字,其他自己改
授权为可执行脚本:chmod 777 stop2start.sh
运行脚本: ./stop2start.sh

升级版本, 增加日期备份

#!/bin/bash
echo "[ --更新项目-- ]"

bakPath="bak/ROOT`date '+%Y%m%d'`"
if  [ -d "$bakPath" ]; then
	n=2
	while [ -d "$bakPath" ];do
		bakPath="bak/ROOT`date '+%Y%m%d'`_$n"
		n=$(($n+1))
	done
fi
if [ -d "ROOT" ]; then
        rm -rf "ROOT"
fi

mkdir ${bakPath} -p
cp msjt_96271.jar ${bakPath}
mv msjt_96271.jar /opt/msjt/static/jar
echo "[ 备份结束 ]"

sp_pid=`ps -ef | grep msjt_96271 | grep -v grep | awk '{print $2}'`
if [ -z "$sp_pid" ];
then
 echo "[ 未查询到 pid ]"
else
 echo "项目进程: $sp_pid "
 kill -9 $sp_pid
fi

baktime="msjt_`date '+%Y%m%d %H:%M:%S'`.out"
mkdir -p /opt/msjt/logs/$baktime
mv /opt/msjt/logs/nohup.out /opt/msjt/logs/$baktime
cp /opt/msjt/logs/$baktime ${bakPath}
echo "[ 项目已关闭 ]"

echo "[ 启动项目 ]"
nohup java -jar /opt/msjt/static/jar/msjt_96271.jar --spring.profiles.active=pro --scheduleIsRun=true > /opt/msjt/logs/nohup.out 2>&1  &
tail -f /opt/msjt/logs/nohup.out
posted @ 2019-09-19 17:29  林宇风  阅读(506)  评论(0编辑  收藏  举报