periodic.sh revision 231568
1#!/bin/sh -
2#
3# $FreeBSD: head/usr.sbin/periodic/periodic.sh 231568 2012-02-12 23:18:05Z brooks $
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
17output_pipe()
18{
19    # Where's our output going ?
20    eval output=\$${1##*/}_output
21    case "$output" in
22    /*) pipe="cat >>$output";;
23    "") pipe=cat;;
24    *)  pipe="mail -E -s '$host ${1##*/} run output' $output";;
25    esac
26    eval $pipe
27}
28
29if [ $# -lt 1 ] ; then
30    usage
31fi
32
33# If possible, check the global system configuration file, 
34# to see if there are additional dirs to check
35if [ -r /etc/defaults/periodic.conf ]; then
36    . /etc/defaults/periodic.conf
37    source_periodic_confs
38fi
39
40host=`hostname`
41export host
42
43# If we were called normally, then create a lock file for each argument
44# in turn and reinvoke ourselves with the LOCKED argument.  This prevents
45# very long running jobs from being overlapped by another run as this is
46# will lead the system running progressivly slower and more and more jobs 
47# are run at once.
48if [ $1 != "LOCKED" ]; then
49    ret=0
50    for arg; do
51        lockfile=/var/run/periodic.${arg##*/}.lock
52        lockf -t 0 "${lockfile}" /bin/sh $0 LOCKED "$arg"
53        case $? in
54        0) ;;
55        73) #EX_CANTCREATE
56            echo "can't create ${lockfile}" | output_pipe $arg
57            ret=1
58            ;;
59        75) #EX_TEMPFAIL
60            echo "$host ${arg##*/} prior run still in progress" | \
61                output_pipe $arg
62            ret=1
63            ;;
64        *)
65            ret=1
66            ;;
67        esac
68    done
69    exit $ret
70fi
71
72if [ $# -ne 2 ]; then
73    usage
74fi
75shift
76arg=$1
77
78tmp_output=`mktemp ${TMPDIR:-/tmp}/periodic.XXXXXXXXXX`
79
80# Execute each executable file in the directory list.  If the x bit is not
81# set, assume the user didn't really want us to muck with it (it's a
82# README file or has been disabled).
83
84success=YES info=YES badconfig=NO empty_output=YES	# Defaults when ${run}_* aren't YES/NO
85for var in success info badconfig empty_output; do
86    case $(eval echo "\$${arg##*/}_show_$var") in
87    [Yy][Ee][Ss]) eval $var=YES;;
88    [Nn][Oo])     eval $var=NO;;
89    esac
90done
91
92case $arg in
93/*) if [ -d "$arg" ]; then
94        dirlist="$arg"
95    else
96        echo "$0: $arg not found" >&2 
97        continue
98    fi
99    ;;
100*)  dirlist=
101    for top in /etc/periodic ${local_periodic}; do
102        [ -d $top/$arg ] && dirlist="$dirlist $top/$arg"
103    done
104    ;;
105esac
106
107{
108    empty=TRUE
109    processed=0
110    for dir in $dirlist; do
111        for file in $dir/*; do
112            if [ -x $file -a ! -d $file ]; then
113                output=TRUE
114                processed=$(($processed + 1))
115                $file </dev/null >$tmp_output 2>&1
116                rc=$?
117                if [ -s $tmp_output ]; then
118                    case $rc in
119                    0)  [ $success = NO ] && output=FALSE;;
120                    1)  [ $info = NO ] && output=FALSE;;
121                    2)  [ $badconfig = NO ] && output=FALSE;;
122                    esac
123                    [ $output = TRUE ] && { cat $tmp_output; empty=FALSE; }
124                fi
125                cp /dev/null $tmp_output
126            fi
127        done
128    done
129    if [ $empty = TRUE ]; then
130        if [ $empty_output = TRUE ]; then
131            [ $processed = 1 ] && plural= || plural=s
132            echo "No output from the $processed file$plural processed"
133        fi
134    else
135        echo ""
136        echo "-- End of $arg output --"
137    fi
138} | output_pipe ${arg}
139
140rm -f $tmp_output
141