Merge from vendor branch LESS:
[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 # $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
12 usage () {
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
18 if [ $# -lt 1 ] ; then
19     usage
20 fi
21
22 # If possible, check the global system configuration file, 
23 # to see if there are additional dirs to check
24 if [ -r /etc/defaults/periodic.conf ]; then
25     . /etc/defaults/periodic.conf
26     source_periodic_confs
27 fi
28
29 host=`hostname`
30 export host
31 tmp_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
37 for arg
38 do
39     # Where's our output going ?
40     eval output=\$${arg##*/}_output
41     case "$output" in
42     /*) pipe="cat >>$output";;
43     "") pipe=cat;;
44     *)  pipe="mail -s '$host ${arg##*/} run output' $output";;
45     esac
46
47     success=YES info=YES badconfig=NO   # Defaults when ${run}_* aren't YES/NO
48     for var in success info badconfig
49     do
50         case $(eval echo "\$${arg##*/}_show_$var") in
51         [Yy][Ee][Ss]) eval $var=YES;;
52         [Nn][Oo])     eval $var=NO;;
53         esac
54     done
55
56     case $arg in
57     /*) if [ -d "$arg" ]
58         then
59             dirlist="$arg"
60         else
61             echo "$0: $arg not found" >&2 
62             continue
63         fi;;
64     *)  dirlist=
65         for top in /etc/periodic ${local_periodic}
66         do
67             [ -d $top/$arg ] && dirlist="$dirlist $top/$arg"
68         done;;
69     esac
70
71     {
72         empty=TRUE
73         processed=0
74         for dir in $dirlist
75         do
76             for file in $dir/*
77             do
78                 if [ -x $file -a ! -d $file ]
79                 then
80                     output=TRUE
81                     processed=$(($processed + 1))
82                     $file </dev/null >$tmp_output 2>&1
83                     rc=$?
84                     if [ -s $tmp_output ]
85                     then
86                       case $rc in
87                       0)  [ $success = NO ] && output=FALSE;;
88                       1)  [ $info = NO ] && output=FALSE;;
89                       2)  [ $badconfig = NO ] && output=FALSE;;
90                       esac
91                       [ $output = TRUE ] && { cat $tmp_output; empty=FALSE; }
92                     fi
93                     cp /dev/null $tmp_output
94                 fi
95             done
96         done
97         if [ $empty = TRUE ]
98         then
99           [ $processed = 1 ] && plural= || plural=s
100           echo "No output from the $processed file$plural processed"
101         else
102           echo ""
103           echo "-- End of $arg output --"
104         fi
105     } | eval $pipe
106 done
107 rm -f $tmp_output