Yum update 를 자동으로 실행하기

개요

crontab 을 이용해서 yum automatic update 를 자동으로 하는 방법 정리

설정

  1. vi /usr/bin/yum-check 를 한후에 다음 내용을 붙여넣기

     Click here to expand...
    /usr/bin/yum-check
    #!/bin/sh
    #
    # Name:         yum-check
    # Author:       Michael Heiming - 2005-03-11
    # Function:     Run from cron to check for yum updates
    #               and mail results
    # Version:      0.7 (initial)
    # 2005-03-12    0.8 randomize startup (cron only)
    # Config:       /etc/sysconfig/yum
    
    # Pull in sysconfig settings
    
    . /etc/sysconfig/yum-check
    
    maila=${MAILTO:=root}
    yumdat="/tmp/yum-check-update.$$"
    yumb="/usr/bin/yum"
    
    #  wait a random interval if there is not a controlling terminal, 
    #  for load management
    if ! [ -t ]
    then
             num=$RANDOM
             let "num %= ${RANGE:=1}"
             sleep $num
    fi
    
    rm -f ${yumdat%%[0-9]*}*
    
    $yumb check-update >& $yumdat
    
    yumstatus="$?"
    
    case $yumstatus in
             100)
                      cat $yumdat |\
                      mail -s "Alert ${HOSTNAME} updates available!" $maila
                      exit 0
    ;;
             0)
                     # Only send mail if debug is turned on
                     if [ ${CHECKWRK} = "yes" ];then
                     cat $yumdat |\
                     mail -s "Yum check succeeded ${HOSTNAME} zero patches
    available." $maila
                     fi
                     exit 0
    ;;
             *)
                     # Unexpected yum return status
                     (echo "Undefined, yum return status: ${yumstatus}" && \
                     [ -e "${yumdat}" ] && cat "${yumdat}" )|\
                     mail -s "Alert ${HOSTNAME} problems running yum." $maila
    esac
    
    [ -e "${yumdat}" ] && rm ${yumdat}
  2. 실행 속성 부여

    chmod +x /usr/bin/yum-check
  3. vi /etc/sysconfig/yum-check

     Click here to expand...
    /etc/sysconfig/yum-check
    # yes sets yum to check for updates and mail only if patches are available
    # no does enable autoupdate if /var/lock/subsys/yum is available
    CHECKONLY="yes"
    # defaults to root, leave empty if .forward/alias in place for root
    MAILTO="myemail@example.com"
    # Set to yes for debugging only! You'll get a mail for each run!
    CHECKWRK="no"
    # Seconds to randomize startup, if running from cron to balance load
    RANGE="3600"
  4. vi /etc/cron.daily/yum.cron

     Click here to expand...
    /etc/cron.daily/yum.cron
    #!/bin/sh
    
    # Pull in sysconfig settings
    
    . /etc/sysconfig/yum-check
    
    
    if [ -f /var/lock/subsys/yum ]; then
    
             if [ ${CHECKONLY} = "yes" ];then
    
                    /usr/bin/yum-check
             fi
             else
                    /usr/bin/yum -R 10 -e 0 -d 0 -y update yum
                    /usr/bin/yum -R 120 -e 0 -d 0 -y update
    fi
  5. chmod +x /etc/cron.daily/yum.cron

cron.daily 에 등록되었으므로 매일 yum update가 자동으로 실행된다.

 

Ref