必须学会的Linux下一键部署项目的shell脚本

必须学会的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下,而jarproject下,所以在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

  • 12
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值