必须学会的Linux下一键部署项目的shell脚本
在公司也经历了几次项目更新,往服务器上部署上线。然鹅每次都是用的服务器上已经存在的脚本文件,类似这个样子:
每次我就上传个jar包,然后执行
./restart
就结束所谓的发包,太无聊了, 于是想着为什么要用别人的脚本文件呢? 于是有了本篇博客。
基础知识
- 需要用到linux基本命令
- 怎么执行shell脚本
- 使用nohup启动jar包
- dirname命令
- readlink命令
- 基本shell语法,如方法,返回值,if 判断
开始编写脚本
application.sh 程序控制相关脚本
application.sh作为基本模块,里面包含了控制程序的启动,暂停,获取程序状态,以及程序运行的进程id, application.sh将被导入其它的脚本中,使得其它脚本可以执行application.sh里的方法。
readlink -f $0
是取得脚本的相关描述信息,$0
表示正在执行脚本名称。在这里$0
的值就是application.sh
,app_name
表示应用名称,部署不同的jar包时,只需更改app_name
的值即可。我写了注释,应该能看明白。
注意:application.sh里面没有入口函数,只是提供方法供其它模块使用。
#!/bin/bash
#name: application.sh
#desc: 应用相关信息
#dete: 2020/1/9
#author: uwei
#当前脚本所在文件夹路径
current_path=dirname $(readlink -f $0)
#应用名称
app_name="demo.jar"
#应用位置
app_path=$current_path/project/$app_name
#应用运行的pid
pid=''
#判断jar包是否在运行
#jar如果在运行返回1,否则返回0
function is_running(){
pid=`ps aux|grep ${app_name}|grep -v grep|awk '{print $2}'` #获取jar包运行的pid
if [ -z $pid ] ;then #如果pid的长度为空,说明没有在运行
return 0
else
return 1
fi
}
#启动jar包,启动成功返回1,否则返回0
function start(){
#先判断jar有没有在运行
is_running
if [ $? -eq "0" ]; then #没有运行,就使用nohup命令启动
nohup java -jar $app_path > /dev/null 2>&1 &
return 1
else
return 0
fi
}
#停止jar包,停止成功返回1,否则返回0
function stop(){
#先判断jar是否在运行
is_running
if [ $? -eq '0' ]; then
return 0 #jar没有运行,停止失败
else
kill -9 $pid #jar在运行,使用kill命令停止
return 1
fi
}
start.sh 启动脚本
这里需要注意一下
start.sh
就是导入application.sh
, 因此可以直接调用application.sh
里面的方法, 也可以访问里面的变量。
#!/bin/bash
#name: start.sh
#desc: 启动jar包
#date:2020/1/9
#author:uwei
#当前脚本所在目录的路径
current_path=dirname $(readlink -f $0)
#导入application.sh
. $current_path/application.sh
#启动程序
function start_app(){
start
is_running #更新pid
if [ $? -eq '1' ]; then #如果启动成功
echo "${app_name} is started at pid: ${pid}"
else #启动失败
if [ -z $pid ];then #如果pid为空,说明启动失败
echo "${app_name} started failed"
else
echo "${app_name} is already running at pid: ${pid}" #程序已经在运行
fi
fi
}
##############脚本程序入口############
start_app
stop.sh 停止脚本
#!/bin/bash
#name: stop.sh
#desc: 程序暂停脚本
#date: 2020/1/9
#author: uwei
#当前脚本所在目录的路径
current_path=dirname $(readlink -f $0)
#导入application.sh
. $current_path/application.sh
#暂定方法
function stop_app(){
stop
if [ $? -eq '0' ]; then #如果暂停失败
echo "${app_name} is NOT running" #程序没有在运行
else
echo "${app_name} has benn stopped"。 #成功停止
fi
}
#############程序入口###############
stop_app
restart.sh 重启脚本
#!/bin/bash
#name: restart.sh
#desc: 重启脚本
#date: 2020/1/9
#author: uwei
#当前脚本所在目录的路径
current_path=dirname $(readlink -f $0)
#导入application.sh
. $current_path/application.sh
#重启方法
function restart(){
is_running #先获取pid
if [ $? -eq ''0 ] #返回0,表示没有运行,那么我就直接启动
echo "${app_name} is not running, and now it will be started"
start #调用启动方法
is_running #更新pid
echo "${app_name} is running at pid: ${pid}" #提示启动成功
else
stop #程序在运行,先stop,再start
start
is_running #更新pid
echo "${app_name} has benn restarted running at pid: ${pid}"
fi
}
####################程序入口##################
restart
status.sh 获取状态脚本
#!/bin/bash
#name: status.sh
#desc: 获取程序运行状态
#date: 2020/1/9
#author: uwei
#当前脚本所在目录的路径
current_path=dirname $(readlink -f $0)
#导入application.sh
. $current_path/application.sh
#获取程序运行状态方法
function status(){
is_running #获取pid
if [ $? -eq '0' ] #返回0,表示没有运行
echo "${app_name} is not running"
else
echo "${app_name} is running at pid: ${pid}" #返回1,表示在运行
fi
}
################程序入口#################
status
目录结构
scripts
目录里面就是那些.sh
脚本文件,project
目录下放的jar包应用程序:由于
脚本文件
在scripts
下,而jar
在project
下,所以在application.sh
中,是通过这样来定位jar包的:
app_path=$current_path/project/$app_name
尝试一键部署
在
script
目录下,执行
./start.sh
即可启动应用程序执行
./stop.sh
即可关闭应用程序执行
./restart.sh
可以对应用重启执行
./status.sh
查看运行状态
如何用在你的项目上?
按照我的目录结构:
- 拷贝
project目录
和script
目录- 将你的jar包如
xxx.jar
复制到project目录下
- 更改
application.sh
,将里面的app_name='demo.jar'
改成app_name='xxx.jar'
然后就可以一键部署啦···
我将此脚本放在了GitHub上了:https://github.com/uweii/deploy-scripts.git
Over
如果觉得需要更多技术干货, 来我的CSDN 和 GitHub哦
CSDN: https://blog.csdn.net/dummyo
Github: https://github.com/uweii