Blame | Last modification | View Log | RSS feed
#!/bin/bash# Only run if this flag file is set (by /etc/rc.d/init.d/yum-cron)if [ ! -f /var/lock/subsys/yum-cron ]; thenexit 0fiDAILYSCRIPT=/etc/yum/yum-daily.yumWEEKLYSCRIPT=/etc/yum/yum-weekly.yumLOCKDIR=/var/lock/yum-cron.lockLOCKFILE=$LOCKDIR/pidfileTSLOCK=$LOCKDIR/ts.lock# Grab config settingsif [ -f /etc/sysconfig/yum-cron ]; thensource /etc/sysconfig/yum-cronfi# set default for SYSTEMNAME[ -z "$SYSTEMNAME" ] && SYSTEMNAME=$(hostname)# Only run on certain days of the weekdow=`date +%w`DAYS_OF_WEEK=${DAYS_OF_WEEK:-0123456}if [ "${DAYS_OF_WEEK/$dow/}" == "${DAYS_OF_WEEK}" ]; thenexit 0fi# if DOWNLOAD_ONLY is set then we force CHECK_ONLY too.# Gotta check before one can download!if [ "$DOWNLOAD_ONLY" == "yes" ]; thenCHECK_ONLY=yesfiYUMTMP=$(mktemp /var/run/yum-cron.XXXXXX)touch $YUMTMP[ -x /sbin/restorecon ] && /sbin/restorecon $YUMTMP# Random wait functionrandom_wait() {sleep $(( $RANDOM % ($RANDOMWAIT * 60) + 1 ))}# Note - the lockfile code doesn't try and use YUMTMP to email messages nicely.# Too many ways to die, this gets handled by normal cron error mailing.# Try mkdir for the lockfile, will test for and make it in one atomic actionif mkdir $LOCKDIR 2>/dev/null; then# store the current process ID in there so we can check for staleness laterecho "$$" >"${LOCKFILE}"# and clean up locks and tempfile if the script exits or is killedtrap "{ rm -f $LOCKFILE $TSLOCK; rmdir $LOCKDIR 2>/dev/null; rm -f $YUMTMP; exit 255; }" INT TERM EXITelse# lock failed, check if process exists. First, if there's no PID file# in the lock directory, something bad has happened, we can't know the# process name, so clean up the old lockdir and restartif [ ! -f $LOCKFILE ]; thenrmdir $LOCKDIR 2>/dev/nullecho "yum-cron: no lock PID, clearing and restarting myself" >&2exec $0 "$@"fiOTHERPID="$(cat "${LOCKFILE}")"# if cat wasn't able to read the file anymore, another instance probably is# about to remove the lock -- exit, we're *still* lockedif [ $? != 0 ]; thenecho "yum-cron: lock failed, PID ${OTHERPID} is active" >&2exit 0fiif ! kill -0 $OTHERPID &>/dev/null; then# lock is stale, remove it and restartecho "yum-cron: removing stale lock of nonexistant PID ${OTHERPID}" >&2rm -rf "${LOCKDIR}"echo "yum-cron: restarting myself" >&2exec $0 "$@"else# Remove stale (more than a day old) lockfilesfind $LOCKDIR -type f -name 'pidfile' -amin +1440 -exec rm -rf $LOCKDIR \;# if it's still there, it wasn't too old, bailif [ -f $LOCKFILE ]; then# lock is valid and OTHERPID is active - exit, we're locked!echo "yum-cron: lock failed, PID ${OTHERPID} is active" >&2exit 0else# lock was invalid, restartecho "yum-cron: removing stale lock belonging to stale PID ${OTHERPID}" >&2echo "yum-cron: restarting myself" >&2exec $0 "$@"fififi# Then check for updates and/or do them, as configured{# First, if this is CLEANDAY, do soCLEANDAY=${CLEANDAY:-0}if [ ! "${CLEANDAY/$dow/}" == "${CLEANDAY}" ]; then/usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y shell $WEEKLYSCRIPTfi# Now continue to do the real workif [ "$CHECK_ONLY" == "yes" ]; thenrandom_waittouch $TSLOCK/usr/bin/yum $YUM_PARAMETER -e 0 -d 0 -y check-update 1> /dev/null 2>&1case $? in1) exit 1;;100) echo "New updates available for host `/bin/hostname`";/usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y -C check-updateif [ "$DOWNLOAD_ONLY" == "yes" ]; then/usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y --downloadonly updateecho "Updates downloaded, use \"yum -C update\" manually to install them."fi;;esacelif [ "$CHECK_FIRST" == "yes" ]; then# Don't run if we can't access the reposrandom_waittouch $TSLOCK/usr/bin/yum $YUM_PARAMETER -e 0 -d 0 check-update 2>&-case $? in1) exit 1;;100) /usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y update yum/usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y shell $DAILYSCRIPT;;esacelserandom_waittouch $TSLOCK/usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y update yum/usr/bin/yum $YUM_PARAMETER -e ${ERROR_LEVEL:-0} -d ${DEBUG_LEVEL:-0} -y shell $DAILYSCRIPTfi} >> $YUMTMP 2>&1if [ ! -z "$MAILTO" ] && [ -x /bin/mail ]; then# if MAILTO is set, use mail command (ie better than standard mail with cron output)[ -s "$YUMTMP" ] && mail -s "System update: $SYSTEMNAME" $MAILTO < $YUMTMPelse# default behavior is to use cron's internal mailing of output from cron-scriptcat $YUMTMPfirm -f $YUMTMPexit 0