1#!/bin/sh - 2# 3# $FreeBSD: src/usr.sbin/periodic/periodic.sh,v 1.9.2.8 2002/05/21 03:09:35 brian Exp $ 4# $DragonFly: src/usr.sbin/periodic/periodic.sh,v 1.2 2003/06/17 04:29:59 dillon Exp $ 5# 6# Run nightly periodic scripts 7# 8# usage: periodic { daily | weekly | monthly } - run standard periodic scripts 9# periodic /absolute/path/to/directory - run periodic scripts in dir 10# 11 12usage () { 13 echo "usage: $0 <directory of files to execute>" 1>&2 14 echo "or $0 { daily | weekly | monthly }" 1>&2 15 exit 1 16} 17 18if [ $# -lt 1 ] ; then 19 usage 20fi 21 22# If possible, check the global system configuration file, 23# to see if there are additional dirs to check 24if [ -r /etc/defaults/periodic.conf ]; then 25 . /etc/defaults/periodic.conf 26 source_periodic_confs 27fi 28 29host=`hostname` 30export host 31tmp_output=`mktemp ${TMPDIR:-/tmp}/periodic.XXXXXXXXXX` 32 33# Execute each executable file in the directory list. If the x bit is not 34# set, assume the user didn't really want us to muck with it (it's a 35# README file or has been disabled). 36 37for arg 38do 39 # Where's our output going ? 40 eval output=\$${arg##*/}_output 41 case "$output" in 42 /*) pipe="cat >>$output" 43 verbose=YES 44 ;; 45 "") pipe=cat 46 verbose=YES 47 ;; 48 *) pipe="mail -s '$host ${arg##*/} run output' $output" 49 verbose=NO 50 ;; 51 esac 52 53 success=YES info=YES badconfig=NO # Defaults when ${run}_* aren't YES/NO 54 for var in success info badconfig verbose 55 do 56 case $(eval echo "\$${arg##*/}_show_$var") in 57 [Yy][Ee][Ss]) eval $var=YES;; 58 [Nn][Oo]) eval $var=NO;; 59 esac 60 done 61 62 case $arg in 63 /*) if [ -d "$arg" ] 64 then 65 dirlist="$arg" 66 else 67 echo "$0: $arg not found" >&2 68 continue 69 fi;; 70 *) dirlist= 71 for top in /etc/periodic ${local_periodic} 72 do 73 [ -d $top/$arg ] && dirlist="$dirlist $top/$arg" 74 done;; 75 esac 76 77 { 78 empty=TRUE 79 processed=0 80 if [ "$verbose" = YES ] 81 then 82 echo "-- Start of $arg output -- `date`" 83 fi 84 for dir in $dirlist 85 do 86 for file in $dir/* 87 do 88 if [ -x $file -a ! -d $file ] 89 then 90 output=TRUE 91 if [ "$verbose" = YES ] 92 then 93 echo "-- Start of $arg $file output -- `date`" 94 fi 95 processed=$(($processed + 1)) 96 $file </dev/null >$tmp_output 2>&1 97 rc=$? 98 if [ -s $tmp_output ] 99 then 100 case $rc in 101 0) [ $success = NO ] && output=FALSE;; 102 1) [ $info = NO ] && output=FALSE;; 103 2) [ $badconfig = NO ] && output=FALSE;; 104 esac 105 [ $output = TRUE ] && { cat $tmp_output; empty=FALSE; } 106 fi 107 cp /dev/null $tmp_output 108 fi 109 done 110 done 111 if [ $empty = TRUE ] 112 then 113 [ $processed = 1 ] && plural= || plural=s 114 echo "No output from the $processed file$plural processed" 115 else 116 if [ "$verbose" = YES ] 117 then 118 echo "" 119 echo "-- End of $arg output -- `date`" 120 fi 121 fi 122 } | eval $pipe 123done 124rm -f $tmp_output 125