1#!/bin/sh -
2#
3# $FreeBSD: src/usr.sbin/periodic/periodic.sh,v 1.21 2007/06/22 10:04:05 dwmalone 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
11usage () {
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
17if [ $# -lt 1 ] ; then
18    usage
19fi
20
21# If possible, check the global system configuration file, 
22# to see if there are additional dirs to check
23if [ -r /etc/defaults/periodic.conf ]; then
24    . /etc/defaults/periodic.conf
25    source_periodic_confs
26fi
27
28host=`hostname`
29export host
30tmp_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
36for arg
37do
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 -E -s '$host ${arg##*/} run output' $output";;
44    esac
45
46    success=YES info=YES badconfig=NO empty_output=YES	# Defaults when ${run}_* aren't YES/NO
47    for var in success info badconfig empty_output
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        # Print the date at the beginning of all logs (3086063).
72        echo
73        date
74
75        empty=TRUE
76        processed=0
77        for dir in $dirlist
78        do
79            for file in $dir/*
80            do
81                if [ -x $file -a ! -d $file ]
82                then
83                    output=TRUE
84                    processed=$(($processed + 1))
85                    $file </dev/null >$tmp_output 2>&1
86                    rc=$?
87                    if [ -s $tmp_output ]
88                    then
89                      case $rc in
90                      0)  [ $success = NO ] && output=FALSE;;
91                      1)  [ $info = NO ] && output=FALSE;;
92                      2)  [ $badconfig = NO ] && output=FALSE;;
93                      esac
94                      [ $output = TRUE ] && { cat $tmp_output; empty=FALSE; }
95                    fi
96                    cp /dev/null $tmp_output
97                fi
98            done
99        done
100        if [ $empty = TRUE ]
101        then
102          if [ $empty_output = TRUE ]
103          then
104            [ $processed = 1 ] && plural= || plural=s
105            echo "No output from the $processed file$plural processed"
106          fi
107        else
108          echo ""
109          echo "-- End of $arg output --"
110        fi
111    } | eval $pipe
112done
113rm -f $tmp_output
114