home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / usr / lib / cron / run-crons
Text File  |  2010-05-08  |  8KB  |  269 lines

  1. #!/bin/bash
  2. #
  3. # /usr/lib/cron/run-crons
  4. #
  5. # Copyright (c) 1998-2001 SuSE GmbH Nuernberg, Germany. All rights reserved.
  6. #
  7. # this script looks into /etc/cron.{hourly,daily,weekly,monthly} for
  8. # scripts to be executed.  The info about last run is stored in
  9. # /var/spool/cron/lastrun
  10. #
  11. # concept similar to debian and redhat
  12. #
  13. # Changes:
  14. #     1998 - Burchard Steinbild <bs@suse.de>, 1998
  15. #         initial version
  16. #     before 2001 - va@org.chemie.uni-frankfurt.de   
  17. #        send an email with name of date-script instead of cron entry 
  18. #         "Subject: cronjob@www - daily - FAILURE" 
  19. #        (better one script for each date-sub-script)
  20. #        requires changes to /etc/crontab 
  21. #        append > /dev/null 2>&1  to the line calling run-cons 
  22. #     2001-09-11  
  23. #        updated to Suse 7.2 merged  
  24. #     2001-09-12
  25. #        changed FAILURE detection, until now all scripts with output
  26. #        had "failed", now only scripts with error status != 0 
  27. #        have failed.
  28. #     2001-09-13 - ro@suse.de
  29. #        merged with 7.3: call logger with exit value for scripts
  30. #        respect MAILTO as cron does
  31. #        use mktemp -d for all tmpfiles
  32. #        add variable to disable mail if all jobs returned 0
  33. #     2010-04-30 - mseben@novell.com
  34. #        - in "emtpy dir" check don't search in subdirectories and 
  35. #          use-maxdepth 1: LIST=`find $CRONDIR -maxdepth 1 ! -type d` 
  36. #        - added check if SCRIPT exists, to prevent executing SCRIPT expanded 
  37. #          to e.g. /etc/cron.hourly/*  : [ -d $SCRIPT -o ! -e $SCRIPT ] && continue
  38. #          (bnc#600569)
  39. #        - in $SCRIPT case check, removed unneeded ".svn" dir check : 
  40. #          droped " .svn) continue ;;"
  41. #
  42. #
  43. # Don't run jobs on laptops, that are AC-offline
  44. #
  45. if test -x /usr/bin/on_ac_power ; then
  46.     on_ac_power -q
  47.     if [ "$?" = "1" ]; then
  48.         exit 0
  49.     fi
  50. fi
  51.  
  52. if [ -f /etc/sysconfig/cron ]; then
  53.     . /etc/sysconfig/cron
  54. fi
  55.  
  56. BASENAME=`/usr/bin/basename $0`
  57. LOGGER="/bin/logger -t $BASENAME[$$]"
  58.  
  59. export LC_TIME=POSIX
  60. TMPDIR=`mktemp -d /tmp/run-crons.XXXXXX`
  61. trap "rm -rf $TMPDIR" 0 1 2 3 13 15
  62.  
  63. # We will force to run cron.daily after 14 days, even
  64. # if you set MAX_NOT_RUN in /etc/sysconfig/cron
  65. # value is in minutes
  66. MAX_NOT_RUN_FORCE="20160"
  67.  
  68. # Priority change for sub scripts.
  69. # range: highest -20 ... 19 lowest prioriy
  70. #  default processes start in level 10
  71. CRON_SCRIPT_NICE_VALUE=15
  72. SPOOL=/var/spool/cron/lastrun
  73.  
  74. # CRON Result EMail is sent to
  75. if test -z "$MAILTO" ; then
  76.   SEND_TO="root"
  77. else
  78.   SEND_TO="$MAILTO"
  79. fi
  80.  
  81. mkdir -p $SPOOL
  82.  
  83. #set verbose
  84. ## stage 1,  search directories/scripts to run
  85. RUN=""
  86. for CRONDIR in /etc/cron.{hourly,daily,weekly,monthly} ; do
  87.     test -d $CRONDIR || continue
  88.     BASE=${CRONDIR##*/}
  89.     TIME_EXT=${BASE##cron.}
  90.     
  91.     test -e $SPOOL/$BASE && {
  92.         case $BASE in
  93.           cron.hourly)  TIME="-cmin  +60 -or -cmin  60" ;;
  94.           cron.daily) 
  95.             # if DAILY_TIME set, run only at a fixed time of day
  96.             if [ "$DAILY_TIME" != "" ] ; then
  97.                           DAILY_TIME_NEW="`echo $DAILY_TIME | sed s,:,, | sed s,^0\*,, `" 
  98.               test -z "$DAILY_TIME_NEW" && DAILY_TIME_NEW=0
  99.                           if [ "$DAILY_TIME_NEW" -gt "2359" ] ; then
  100.                             echo "wrong time format in /etc/sysconfig/cron DAILY_TIME, value is $DAILY_TIME" | logger
  101.               fi  
  102.                           NOW_H=`date +%H%M| sed s,^0\*,,`
  103.               test -z "$NOW_H" && NOW_H=0
  104.                           if [ $DAILY_TIME_NEW -gt $(($NOW_H-15)) ] && [ $DAILY_TIME_NEW -le $NOW_H ]; then
  105.                               TIME=""
  106.                           else
  107.                   # take care of MAX_NOT_RUN, default is 7 days
  108.                   if [ "$MAX_NOT_RUN" != "0" ] ; then
  109.                                 TIME="-cmin +$((1440*$MAX_NOT_RUN)) -or -cmin $((1440*$MAX_NOT_RUN))"
  110.                   else
  111.                 TIME="-cmin +$MAX_NOT_RUN_FORCE -or -cmin $MAX_NOT_RUN_FORCE" 
  112.                   fi                               
  113.                           fi 
  114.  
  115.             # run as usual   
  116.             else
  117.               TIME="-cmin +1440 -or -cmin 1440" 
  118.             fi ;;
  119.           cron.weekly)  TIME="-cmin +10080 -or -cmin 10080"  ;;
  120.           cron.monthly) 
  121.             DAYOFMONTH=`date '+%d'`
  122.             DAYSLASTMONTH=`date -d "-$DAYOFMONTH days" '+%d'`
  123.             if [ $DAYOFMONTH -gt $DAYSLASTMONTH ] ; then
  124.                 LASTMONTHSTR="-$DAYOFMONTH days"
  125.             else
  126.                 LASTMONTHSTR="last month"
  127.             fi
  128.             NOW=`date +%s`
  129.             LASTMONTH=`date -d "$LASTMONTHSTR" +%s`
  130.                         DIFF=`expr '(' $NOW - $LASTMONTH ')' / 86400`
  131.                         TIME="-ctime +$DIFF"
  132.                         ;;
  133.         esac
  134.     # remove all lock files for scripts that are due to run
  135.         eval find $SPOOL/$BASE $TIME | \
  136.              xargs --no-run-if-empty rm
  137.     }
  138.     if test ! -e $SPOOL/$BASE ; then
  139.     # accept this dir, if it isn't empty, don't search for files in subdirectories    
  140.     LIST=`find $CRONDIR -maxdepth 1 ! -type d`
  141.     if [ ! -z "$LIST" ] ; then
  142.         RUN="${RUN} ${TIME_EXT}"
  143.     fi
  144.     fi
  145. done
  146.  
  147.  
  148. ##  STATUS communication variable between 
  149. #     function run_scripts ()
  150. #   and loop-over-all-scripts 
  151. #   set in run_scripts to FAILURE if this script failed!
  152. #   else it is empty 
  153. #   because it is never reset to empty after the initialization
  154. #   it implements an OR like logic over all scripts
  155. ##  
  156. STATUS=""
  157.  
  158. # helper, run all scripts in one cron directory
  159. function run_scripts (){
  160.     local CRONDIR=$1
  161.     local TIME_EXT=$2
  162.  
  163.  
  164.     local TEMP_MSG=$TMPDIR/run-crons.${TIME_EXT}.$$
  165.     rm -r $TMPDIR/run-crons.${TIME_EXT}.* >/dev/null 2>&1
  166.  
  167.     # keep going when something fails
  168.     set +e
  169.     for SCRIPT in $CRONDIR/* ; do
  170.     # skip directories and empty crondir (SCRIPT expanded 
  171.     # to e.g. /etc/cron.hourly/*)
  172.     [ -d $SCRIPT -o ! -e $SCRIPT ] && continue
  173.     case "$SCRIPT" in
  174.                *.rpm*)         continue ;;
  175.                *.swap)         continue ;;
  176.                *.bak)          continue ;;
  177.                *.orig)         continue ;;
  178.                \#*)            continue ;;
  179.                *~)             continue ;;
  180.         esac
  181.     /sbin/checkproc $SCRIPT && continue
  182.     if test -x $SCRIPT ; then
  183.         BASESCRIPT=`/usr/bin/basename $SCRIPT`
  184.  
  185.         nice -n ${CRON_SCRIPT_NICE_VALUE} $SCRIPT >$TEMP_MSG 2>&1
  186.  
  187.         local ERRNO=$?
  188.         if [ 0 -eq $ERRNO ] ; then    
  189.         if [ "$SYSLOG_ON_NO_ERROR" = "yes" ]; then
  190.             echo "$BASESCRIPT: OK" | $LOGGER -p info
  191.         fi
  192.         else
  193.         echo "$BASESCRIPT returned $ERRNO" | $LOGGER -p warn
  194.         echo "SCRIPT: $BASESCRIPT exited with RETURNCODE = $ERRNO." 
  195.                    STATUS="FAILURE"
  196.         fi
  197.         # write some wrapper text around the original output
  198.         if [ -s "$TEMP_MSG" ] ; then 
  199.         echo "SCRIPT: output (stdout && stderr) follows"
  200.         echo ""
  201.         cat $TEMP_MSG         
  202.         echo -e "SCRIPT: $BASESCRIPT\n------- END OF OUTPUT"     
  203.             echo ""     
  204.         echo ""     
  205.         fi  
  206.             
  207.         rm -f $TEMP_MSG > /dev/null 2>&1
  208.     else
  209.         echo "WARNING: $SCRIPT is not executable, script is ignored !"
  210.     fi
  211.     done
  212. }
  213.  
  214. # stage 2: 
  215. #   run all scripts  and collect output into one mail
  216. #   for each TIME_EXT with a meaningfull subject.
  217. #        
  218. if [ ! -z "${RUN}"  ] ; then
  219.     for EXT in ${RUN} ; do
  220.     CRONDIR="/etc/cron."${EXT}
  221.     test -d $CRONDIR || continue
  222.     BASE=`/usr/bin/basename $CRONDIR`
  223.     TIME_EXT=${BASE##cron.}
  224.     STATUS=""
  225.  
  226.     if test ! -e $SPOOL/$BASE ; then
  227.         CONTROL_MAIL=$TMPDIR/run-crons_mail.$$
  228.         JOB_OUTPUT=$TMPDIR/run-crons_output.$$
  229.  
  230.         echo "running ${TIME_EXT} cronjob scripts" >> ${CONTROL_MAIL}
  231.         echo "" >> ${CONTROL_MAIL}
  232.  
  233.         touch $SPOOL/$BASE
  234.  
  235.         run_scripts  ${CRONDIR} ${TIME_EXT} >> ${JOB_OUTPUT} 2>&1
  236.         TITLE="cronjob@$HOSTNAME - ${TIME_EXT}"
  237.         if [ -n "${STATUS}" ] ; then 
  238.         TITLE="${TITLE} - ${STATUS}" 
  239.             else 
  240.                 TITLE="${TITLE} - OK" 
  241.         fi
  242.         if [ -n "${STATUS}" -o "$SEND_MAIL_ON_NO_ERROR" = "yes" ] ; then
  243.         cat ${CONTROL_MAIL} ${JOB_OUTPUT} | mail ${SEND_TO} -s "${TITLE}" 
  244.         elif [ -s ${JOB_OUTPUT} -a "$SEND_OUTPUT_ON_NO_ERROR" = "yes" ] ; then
  245.         cat ${CONTROL_MAIL} ${JOB_OUTPUT} | mail ${SEND_TO} -s "${TITLE}" 
  246.         fi
  247.         
  248.         rm -f ${CONTROL_MAIL} ${JOB_OUTPUT}
  249.     fi
  250.     done
  251. fi
  252.  
  253. #
  254. # now make sure, we have no lastrun files dated to future
  255. #
  256. touch $SPOOL
  257. NOW=`date -u +%s`
  258. for i in `find $SPOOL -type f`
  259. do
  260.     FILEDATE=`date -u -r $i +%s`
  261.     # allow for up to one hour in the future because of summer/wintertime
  262.     if [ $((FILEDATE - NOW)) -gt 3600 ]
  263.     then
  264.     rm $i
  265.     fi
  266. done
  267.  
  268.  
  269.