微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

实例脚本,判断是否加入开机自启动,服务状态、脚本规范

脚本实例:

  • 判断ntpd服务是否加入开机自启动


  • #!/bin/bash
    #Output:
    #Resultmustexactlyequalto"3:on,5:on|enable"
    #
    #Otheroutputisnon-compliant.
    
    #ConfirmOsversion
    unsetoS_VERSION
    
    uname-r|grepel5>/dev/null&&OS_VERSION=el5
    uname-r|grepel6>/dev/null&&OS_VERSION=el6
    uname-r|grepel7>/dev/null&&OS_VERSION=el7
    
    #Checkntpautostartonrhel5andrhel6
    functionntp_boot_start()
    {
    localLANG
    localret
    
    LANG="en_US.UTF-8"
    ret=$(chkconfig--listntpd2>/dev/null|awk'{print$5","$7}')
    
    	if["$ret"=""];then
    		ntp_auto=false
    		echo"$ntp_auto"
    
    	elif["$ret"!=""-a"$ret"!="3:on,5:on"];then
    #		echo"$ret"
    #echo"Check[ntpdautostart]...Failed"
    		ntp_auto=false
    		echo"$ntp_auto"
    
    	else
    #		echo"$ret"
    		ntp_auto=true
    		echo"$ntp_auto"
    	fi
    }
    
    #Checkchronyautostartonrhel7
    functionchrony_boot_start()
    {
    localLANG
    localret
    
    LANG="en_US.UTF-8"
    ret=$(systemctlis-enabledchronyd.service2>/dev/null)
    
    	if["$ret"=""];then
    		ntp_auto=false
    		echo"$ntp_auto"
    	elif["$ret"!=""-a"$ret"!="enabled"];then
    		ntp_auto=false
    		echo"$ntp_auto"
    #echo"Changemethod:"
    		#echo"systemctlenablechronyd.service"
    	else
    		ntp_auto=true
    		echo"$ntp_auto"
    	fi
    }
    
    #Begincheck
    if["$OS_VERSION"="el5"-o"$OS_VERSION"="el6"];then
    ntp_boot_start
    elif["$OS_VERSION"="el7"];then
    chrony_boot_start
    fi


  • 判断服务状态


  • #!/bin/bash
    #Output:
    #Atthistime,itmustexactlyequalto"UP".
    #
    #Otheroutputisnon-compliant.
    
    #ConfirmOsversion
    unsetoS_VERSION
    
    uname-r|grepel5>/dev/null&&OS_VERSION=el5
    uname-r|grepel6>/dev/null&&OS_VERSION=el6
    uname-r|grepel7>/dev/null&&OS_VERSION=el7
    
    #Begincheck
    if["$OS_VERSION"="el5"-o"$OS_VERSION"="el6"];then
    pidofntpd&>/dev/null
    if[$?-ne0];then
    		ntpd_service_status=flase
    echo"ntpd_service_status$ntpd_service_status"
    #echo"DOWN"
    #echo"Check[ntpservicestatus]...Failed"
    #echo"Changemethod:"
    #echo"servicentpdstart"
    else
    		ntpd_service_status=true
    echo"ntpd_service_status$ntpd_service_status"
    #echo"UP"
    fi
    elif["$OS_VERSION"="el7"];then
    pidofchronyd&>/dev/null
    if[$?-ne0];then
    		ntpd_service_status=flase
    echo"ntpd_service_status$ntpd_service_status"
    #echo"DOWN"
    #echo"Check[chronyservicestatus]...Failed"
    #echo"Changemethod:"
    #echo"systemctlstartchronyd.service"
    else
    		ntpd_service_status=true
    echo"ntpd_service_status$ntpd_service_status"
    #echo"UP"
    fi
    fi

原文地址:https://www.jb51.cc/bash/391567.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐