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

前端之家收集整理的这篇文章主要介绍了实例脚本,判断是否加入开机自启动,服务状态、脚本规范前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

脚本实例:

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


    1. #!/bin/bash
    2. #Output:
    3. #Resultmustexactlyequalto"3:on,5:on|enable"
    4. #
    5. #Otheroutputisnon-compliant.
    6.  
    7. #ConfirmOSVersion
    8. unsetOS_VERSION
    9.  
    10. uname-r|grepel5>/dev/null&&OS_VERSION=el5
    11. uname-r|grepel6>/dev/null&&OS_VERSION=el6
    12. uname-r|grepel7>/dev/null&&OS_VERSION=el7
    13.  
    14. #Checkntpautostartonrhel5andrhel6
    15. functionntp_boot_start()
    16. {
    17. localLANG
    18. localret
    19.  
    20. LANG="en_US.UTF-8"
    21. ret=$(chkconfig--listntpd2>/dev/null|awk'{print$5","$7}')
    22.  
    23. if["$ret"=""];then
    24. ntp_auto=false
    25. echo"$ntp_auto"
    26.  
    27. elif["$ret"!=""-a"$ret"!="3:on,5:on"];then
    28. # echo"$ret"
    29. #echo"Check[ntpdautostart]...Failed"
    30. ntp_auto=false
    31. echo"$ntp_auto"
    32.  
    33. else
    34. # echo"$ret"
    35. ntp_auto=true
    36. echo"$ntp_auto"
    37. fi
    38. }
    39.  
    40. #Checkchronyautostartonrhel7
    41. functionchrony_boot_start()
    42. {
    43. localLANG
    44. localret
    45.  
    46. LANG="en_US.UTF-8"
    47. ret=$(systemctlis-enabledchronyd.service2>/dev/null)
    48.  
    49. if["$ret"=""];then
    50. ntp_auto=false
    51. echo"$ntp_auto"
    52. elif["$ret"!=""-a"$ret"!="enabled"];then
    53. ntp_auto=false
    54. echo"$ntp_auto"
    55. #echo"Changemethod:"
    56. #echo"systemctlenablechronyd.service"
    57. else
    58. ntp_auto=true
    59. echo"$ntp_auto"
    60. fi
    61. }
    62.  
    63. #Begincheck
    64. if["$OS_VERSION"="el5"-o"$OS_VERSION"="el6"];then
    65. ntp_boot_start
    66. elif["$OS_VERSION"="el7"];then
    67. chrony_boot_start
    68. fi


  • 判断服务状态


    1. #!/bin/bash
    2. #Output:
    3. #Atthistime,itmustexactlyequalto"UP".
    4. #
    5. #Otheroutputisnon-compliant.
    6.  
    7. #ConfirmOSVersion
    8. unsetOS_VERSION
    9.  
    10. uname-r|grepel5>/dev/null&&OS_VERSION=el5
    11. uname-r|grepel6>/dev/null&&OS_VERSION=el6
    12. uname-r|grepel7>/dev/null&&OS_VERSION=el7
    13.  
    14. #Begincheck
    15. if["$OS_VERSION"="el5"-o"$OS_VERSION"="el6"];then
    16. pidofntpd&>/dev/null
    17. if[$?-ne0];then
    18. ntpd_service_status=flase
    19. echo"ntpd_service_status$ntpd_service_status"
    20. #echo"DOWN"
    21. #echo"Check[ntpservicestatus]...Failed"
    22. #echo"Changemethod:"
    23. #echo"servicentpdstart"
    24. else
    25. ntpd_service_status=true
    26. echo"ntpd_service_status$ntpd_service_status"
    27. #echo"UP"
    28. fi
    29. elif["$OS_VERSION"="el7"];then
    30. pidofchronyd&>/dev/null
    31. if[$?-ne0];then
    32. ntpd_service_status=flase
    33. echo"ntpd_service_status$ntpd_service_status"
    34. #echo"DOWN"
    35. #echo"Check[chronyservicestatus]...Failed"
    36. #echo"Changemethod:"
    37. #echo"systemctlstartchronyd.service"
    38. else
    39. ntpd_service_status=true
    40. echo"ntpd_service_status$ntpd_service_status"
    41. #echo"UP"
    42. fi
    43. fi

猜你在找的Bash相关文章