直接复制粘贴就能使用的 shell 脚本函数②

1、输出 成功 / 失败 的信息

print_success_process()
{
    info=$1
    result=$2
    echo -n ${info}
    count=`expr ${INFOLEN} - ${#info}`
    while [ ${count} -gt 0 ]
    do
        echo -n " "
        count=`expr ${count} - 1`
    done
    echo -e "${COLOR_GREEN}[ ${result} ]${COLOR_WHITE}"
}


print_failure_process()
{
    info=$1
    result=$2
    echo -n ${info}
    count=`expr ${INFOLEN} - ${#info}`
    while [ ${count} -gt 0 ]
    do
        echo -n " "
        count=`expr ${count} - 1`
    done
    echo -e "${COLOR_RED}[ ${result} ]${COLOR_WHITE}"
}

2、启动 java jar 包

## 产品名称
SERVER_NAME_P=product

## SERVER 服务路径
SERVER_HOME_APP=/usr/local/${SERVER_NAME_P}/tomcat

## 配置文件路径
SERVER_HOME_CON=${SERVER_HOME_APP}/config/application-prod.yml



start_show()
{
    progress_name=$1
    ProcNumber=`ps -ef | grep -w ${progress_name} |grep -v grep| wc -l`
    if [ $ProcNumber -le 0 ];then
       print_failure_process "Start service[${progress_name/.jar/}]" "Failed"
    else 
       print_success_process "Start service[${progress_name/.jar/}]" "Succeeded"
    fi
}

 

start_jar()
{

    ##外置依赖 启动参数 JAR包 日志 
    dloadPath=$1
    javaStartParam=$2
    jarName=$3
    logInfo=$4
    sleepTime=$5

    if [ -z "`ps aux|grep java|grep ${jarName}|grep -v grep`" ];then
        nohup java -Dloader.path=${dloadPath} ${javaStartParam} -jar ${SERVER_HOME_APP}/${jarName}  --spring.config.location=${SERVER_HOME_CON} > ${logInfo} 2>&1 &
        sleep ${sleepTime}s  
    fi
    ## 打印服务状态
    start_show "${jarName}"

}

3、杀死指定服务进程

kill_process()
{
    process_name=$1
    process_num=`ps aux|grep ${process_name}|grep -v grep|awk '{print $2}'`
    if [ -n "${process_num}" ];then
        echo ${process_num}|xargs kill -92
        #判断是否成功停止
        sleep 1s
        process_num=`ps aux|grep ${process_name}|grep -v grep|awk '{print $2}'`
        if [ -n "${process_num}" ];then
            print_failure_process "Stop service[${process_name/.jar/}]" "Failed"
        else
            print_success_process "Stop service[${process_name/.jar/}]" "Succeeded"
        fi
    else
        print_success_process "Stop service[${process_name/.jar/}]" "Succeeded"
    fi
}

4、接收 yes no 的函数

## 0表示确定,1表示否
yesnoinput()
{
    while :
    do
        read ANSWER
        case $ANSWER in
        "y"|"Y"|"yes"|"YES")
            return 0
            ;;
        "n"|"N"|"no"|"NO")
            return 1
            ;;
        *)
            echo "Unknown input."
            ;;
        esac
        printf "Please input [yes..no]:"
    done
}

5、检查是否存在当前业务镜像安装包

checkproductpag()
{
    CHECKPDTTEMP=$1
    #查找当前的业务包是否已经有镜像了 如果存在镜像 代表已经安装过镜像包了
    if [ -z "`ls $TOP_PATH | grep image-pro-$CHECKPDTTEMP`" ];then
        #没有找到对应的安装包 则报错误退出
        echoerror "no $CHECKPDTTEMP image was found package, please check."
        
        exit 1
    fi
}

6、检查当前版本业务包镜像是否已经安装 

checkproductimg()
{
    CHECKPDTTEMP=$1

    #查找当前的业务包是否已经有镜像了 如果存在镜像 代表已经安装过镜像包了
    if [ -n "`docker images | grep ${CHECKPDTTEMP}`" ];then
        #提示用户版本已经安装 并退出
        echoerror "${CHECKPDTTEMP} has been installed, no need to install it again."
        
        exit 1
    fi
}

7、删除指定容器 

deleteImageAndContainer()
{

    #镜像名称
    IMAGENAME=$1

    varri=0
    while :
    do
        #杀死正在运行的docker 容器
        if [ ! -z "`docker ps | grep ${IMAGENAME}`" ];then
            RUNCONTAINERINFO=$(docker ps | grep $IMAGENAME)

            IFS=' ' runcontainerarray=($RUNCONTAINERINFO)
            docker kill ${runcontainerarray[0]}

            varri=`expr $varri + 1 `
            if [ "$varri" -eq 5 ]
            then
                break
            fi
        else
            break
        fi
    done

    varri=0
    while :
    do    
        # 删除指定名称的容器

        if [ ! -z "`docker ps -a | grep ${IMAGENAME}`" ];then
            CONTAINERINFO=$(docker ps -a | grep $IMAGENAME)
            IFS=' ' containerarray=($CONTAINERINFO)
            docker rm -f ${containerarray[0]}
            varri=`expr $varri + 1 `
            if [ "$varri" -eq 5 ]
            then
                break
            fi            
        else
           break
        fi
    done 

    varri=0
    while :
    do   
        #删除指定名称的镜像
        if [ ! -z "`docker images| grep ${IMAGENAME}`" ];then
            IMAGEINFO=$(docker images | grep $IMAGENAME)
            IFS=' ' imagearray=($IMAGEINFO)
            docker rmi -f ${imagearray[2]}

            varri=`expr $varri + 1 `
            if [ "$varri" -eq 5 ]
            then
                break
            fi            
        else
            break
        fi
    done 
}

直接复制粘贴就能使用的 shell 脚本函数②_第1张图片

你可能感兴趣的:(shell,Java基础,java,linux,开发语言,运维)