Subversion Repositories configs

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 - 1
#!/bin/bash
2
 
3
# Only run if this flag file is set (by /etc/rc.d/init.d/yum-cron)
4
if [ ! -f /var/lock/subsys/yum-cron ]; then
5
  exit 0
6
fi
7
 
8
DAILYSCRIPT=/etc/yum/yum-daily.yum
9
WEEKLYSCRIPT=/etc/yum/yum-weekly.yum
10
LOCKDIR=/var/lock/yum-cron.lock
11
LOCKFILE=$LOCKDIR/pidfile
12
TSLOCK=$LOCKDIR/ts.lock
13
 
14
# Grab config settings
15
if [ -f /etc/sysconfig/yum-cron ]; then
16
  source /etc/sysconfig/yum-cron
17
fi
18
# set default for SYSTEMNAME
19
[ -z "$SYSTEMNAME" ]  && SYSTEMNAME=$(hostname)
20
 
21
# Only run on certain days of the week
22
dow=`date +%w`
23
DAYS_OF_WEEK=${DAYS_OF_WEEK:-0123456}
24
if [ "${DAYS_OF_WEEK/$dow/}" == "${DAYS_OF_WEEK}" ]; then
25
  exit 0
26
fi
27
 
28
# if DOWNLOAD_ONLY is set then we force CHECK_ONLY too.
29
# Gotta check before one can download!
30
if [ "$DOWNLOAD_ONLY" == "yes" ]; then
31
  CHECK_ONLY=yes
32
fi
33
 
34
YUMTMP=$(mktemp /var/run/yum-cron.XXXXXX)
35
touch $YUMTMP
36
[ -x /sbin/restorecon ] && /sbin/restorecon $YUMTMP
37
 
38
# Random wait function
39
random_wait() {
40
  sleep $(( $RANDOM % ($RANDOMWAIT * 60) + 1 ))
41
}
42
 
43
# Note - the lockfile code doesn't try and use YUMTMP to email messages nicely.
44
# Too many ways to die, this gets handled by normal cron error mailing.
45
# Try mkdir for the lockfile, will test for and make it in one atomic action
46
if mkdir $LOCKDIR 2>/dev/null; then
47
  # store the current process ID in there so we can check for staleness later
48
  echo "$$" >"${LOCKFILE}"
49
  # and clean up locks and tempfile if the script exits or is killed
50
  trap "{ rm -f $LOCKFILE $TSLOCK; rmdir $LOCKDIR 2>/dev/null; rm -f $YUMTMP; exit 255; }" INT TERM EXIT
51
else
52
  # lock failed, check if process exists.  First, if there's no PID file
53
  # in the lock directory, something bad has happened, we can't know the
54
  # process name, so clean up the old lockdir and restart
55
  if [ ! -f $LOCKFILE ]; then
56
    rmdir $LOCKDIR 2>/dev/null
57
    echo "yum-cron: no lock PID, clearing and restarting myself" >&2
58
    exec $0 "$@"
59
  fi
60
  OTHERPID="$(cat "${LOCKFILE}")"
61
  # if cat wasn't able to read the file anymore, another instance probably is
62
  # about to remove the lock -- exit, we're *still* locked
63
    if [ $? != 0 ]; then
64
      echo "yum-cron: lock failed, PID ${OTHERPID} is active" >&2
65
      exit 0
66
    fi
67
    if ! kill -0 $OTHERPID &>/dev/null; then
68
      # lock is stale, remove it and restart
69
      echo "yum-cron: removing stale lock of nonexistant PID ${OTHERPID}" >&2
70
      rm -rf "${LOCKDIR}"
71
      echo "yum-cron: restarting myself" >&2
72
      exec $0 "$@"
73
    else
74
      # Remove stale (more than a day old) lockfiles
75
      find $LOCKDIR -type f -name 'pidfile' -amin +1440 -exec rm -rf $LOCKDIR \;
76
      # if it's still there, it wasn't too old, bail
77
      if [ -f $LOCKFILE ]; then
78
        # lock is valid and OTHERPID is active - exit, we're locked!
79
        echo "yum-cron: lock failed, PID ${OTHERPID} is active" >&2
80
        exit 0
81
      else
82
        # lock was invalid, restart
83
	echo "yum-cron: removing stale lock belonging to stale PID ${OTHERPID}" >&2
84
        echo "yum-cron: restarting myself" >&2
85
        exec $0 "$@"
86
      fi
87
    fi
88
fi
89
 
90
# Then check for updates and/or do them, as configured
91
{
92
  # First, if this is CLEANDAY, do so
93
  CLEANDAY=${CLEANDAY:-0}
94
  if [ ! "${CLEANDAY/$dow/}" == "${CLEANDAY}" ]; then
95
      /usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y shell $WEEKLYSCRIPT
96
  fi
97
 
98
  # Now continue to do the real work
99
  if [ "$CHECK_ONLY" == "yes" ]; then
100
    random_wait
101
    touch $TSLOCK
102
    /usr/bin/yum $YUM_PARAMETER -e 0 -d 0 -y check-update 1> /dev/null 2>&1
103
    case $? in
104
      1)   exit 1;;
105
      100) echo "New updates available for host `/bin/hostname`";
106
           /usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y -C check-update
107
           if [ "$DOWNLOAD_ONLY" == "yes" ]; then
108
	       /usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y --downloadonly update
109
	       echo "Updates downloaded, use \"yum -C update\" manually to install them."
110
	   fi
111
	   ;;
112
    esac
113
  elif [ "$CHECK_FIRST" == "yes" ]; then
114
    # Don't run if we can't access the repos
115
    random_wait
116
    touch $TSLOCK
117
    /usr/bin/yum $YUM_PARAMETER -e 0 -d 0 check-update 2>&-
118
    case $? in
119
      1)   exit 1;;
120
      100) /usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y update yum
121
           /usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y shell $DAILYSCRIPT
122
           ;;
123
    esac
124
  else
125
    random_wait
126
    touch $TSLOCK
127
    /usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y update yum
128
    /usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y shell $DAILYSCRIPT
129
  fi
130
} >> $YUMTMP 2>&1
131
 
132
if [ ! -z "$MAILTO" ] && [ -x /bin/mail ]; then
133
# if MAILTO is set, use mail command (ie better than standard mail with cron output)
134
  [ -s "$YUMTMP" ] && mail -s "System update: $SYSTEMNAME" $MAILTO < $YUMTMP
135
else
136
# default behavior is to use cron's internal mailing of output from cron-script
137
  cat $YUMTMP
138
fi
139
rm -f $YUMTMP
140
 
141
exit 0