1.脚本
# 1. 先将包上传到package目录;(提前完成)
# 2. 重命名之前的文件jar\dir;
# 3. 移动解压上传的包;
# 4. 编辑上传的脚本;
# 5. 找到对应的进程;
# 6. 杀死对应的进程;
# 7. 运行启动脚本;
#2.
vDate=`date +%Y%m%d%H%m`
mv mf-auth-center-server-1.0.0.tar.gz mf-auth-center-server-1.0.0.tar.gz.$vDate
mv mf-auth-center-server-1.0.0 mf-auth-center-server-1.0.0.$vDate
#3.
mv package/mf-auth-center-server-1.0.0.tar.gz mf-auth-center-server-1.0.0.tar.gz
tar -zxvf mf-auth-center-server-1.0.0.tar.gz
#4.
sed -i 's/^M//g' mf-auth-center-server-1.0.0/bin/mfac.sh
#5.
PID=$(ps -ef|grep mf-auth-center-server-1.0.0.jar |grep java|awk '{print $2}')
#6.
if [-z "$PID"]
then
echo port 7071 is already stoped
else
kill -9 ${PID}
echo port 7071 stopped successfully
fi
#7.
sh mf-auth-center-server-1.0.0/bin/mfac.sh start
echo mfac.sh started successfully
运行的jar文件参考:
https://blog.csdn.net/leinminna/article/details/108872816
2. 获取进程id
建议通第二种方式获取进程id,来kill进程,第一种会有重复启动问题,不同用户不会kill系统的进程,会导致启动多个进程,从引发某些问题(日志不同步,部署的代码不生效…)。
2.1通过端口号获取
PID=$(netstat -tnlp|grep 7071|awk '{print $7}'| awk '{split($0,arr,"/")} END {print arr[1]}')
2.2 通过jar获取
PID=$(ps -ef|grep mf-auth-center-server-1.0.0.jar |grep java|awk '{print $2}')
# 或者
PID=$(ps -ef|grep mf-auth-center-server-1.0.0.jar |grep -v grep |awk '{print $2}')
3.脚本替换
解压后的脚本是windows格式的,需要替换为linux可用脚本,有两种方法:
3.1脚本内替换
需要vi进入脚本(替换回车)
vi restart.sh
# ^ (按control+V)
# M (按control+M)
:%s/^M//g
3.2 脚本外替换
不需要进入脚本,在脚本外执行命令即可(替换回车)
# ^ (按control+V)
# M (按control+M)
sed -i 's/^M//g' restart.sh
还可以如下:
sed -i 's/\r//' restart.sh
Windows下换行符号是“\r\n”,Linux下换行符号是“\n”,所以去掉"\r"即可(替换"\r"为空)
3.2相比3.1的优点是便于写整体的脚本;