脚本实例:

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


  • #!/bin/bash
    # Output:
    # Result must exactly equal to "3:on,5:on|enable"
    #
    # Other output is non-compliant.
    
    # Confirm OS Version
    unset OS_VERSION
    
    uname -r | grep el5 > /dev/null && OS_VERSION=el5
    uname -r | grep el6 > /dev/null && OS_VERSION=el6
    uname -r | grep el7 > /dev/null && OS_VERSION=el7
    
    # Check ntp auto start on rhel5 and rhel6
    function ntp_boot_start ()
    {
        local LANG
        local ret
        
        LANG="en_US.UTF-8"
        ret=$(chkconfig --list ntpd 2>/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 [ntpd auto start]... FAILED"
    		ntp_auto=false
    		echo "$ntp_auto"
    
    	else
    #		echo "$ret"
    		ntp_auto=true
    		echo "$ntp_auto"
    	fi
    }
    
    # Check chrony auto start on rhel7
    function chrony_boot_start ()
    {
        local LANG
        local ret
        
        LANG="en_US.UTF-8"
        ret=$(systemctl is-enabled chronyd.service 2>/dev/null)
        
    	if [ "$ret" = "" ]; then
    		ntp_auto=false
    		echo "$ntp_auto"
    	elif [ "$ret" != "" -a "$ret" != "enabled" ];then
    		ntp_auto=false
    		echo "$ntp_auto"
            #echo "Change method:"
    		#echo "systemctl enable chronyd.service"
    	else
    		ntp_auto=true
    		echo "$ntp_auto"
    	fi
    }
    
    #Begin check
    if [ "$OS_VERSION" = "el5" -o "$OS_VERSION" = "el6" ];then
        ntp_boot_start
    elif [ "$OS_VERSION" = "el7" ];then
        chrony_boot_start
    fi


  • 判断服务状态


  • #!/bin/bash
    # Output:
    # At this time, it must exactly equal to "UP".
    # 
    # Other output is non-compliant.
    
    # Confirm OS Version
    unset OS_VERSION
    
    uname -r | grep el5 > /dev/null && OS_VERSION=el5
    uname -r | grep el6 > /dev/null && OS_VERSION=el6
    uname -r | grep el7 > /dev/null && OS_VERSION=el7
    
    # Begin check 
    if [ "$OS_VERSION" = "el5" -o "$OS_VERSION" = "el6" ];then
        pidof ntpd &> /dev/null
        if [ $? -ne 0 ];then
    		ntpd_service_status=flase
            echo "ntpd_service_status $ntpd_service_status"
    #        echo "DOWN"
    #        echo "Check [ntp service status]... FAILED"
            #echo "Change method:"
            #echo "service ntpd start"
        else
    		ntpd_service_status=true
            echo "ntpd_service_status $ntpd_service_status"
    #        echo "UP"
        fi
    elif [ "$OS_VERSION" = "el7" ];then
        pidof chronyd &> /dev/null
        if [ $? -ne 0 ];then
    		ntpd_service_status=flase
            echo "ntpd_service_status $ntpd_service_status"
    #        echo "DOWN"
    #        echo "Check [chrony service status]... FAILED"
            #echo "Change method:"
            #echo "systemctl start chronyd.service"
        else
    		ntpd_service_status=true
            echo "ntpd_service_status $ntpd_service_status"
    #        echo "UP"
        fi
    fi