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