2 # /nightlies/cron_wrapper.sh
4 # check for no argument case and stop
10 # set permanent $LOG file var
11 LOG="/var/log/crontab-nightlies-log/crontab.log"
12 # set $LOG_LOCKED_ERR in case locking failed
13 LOG_LOCK_ERR="/var/log/crontab-nightlies-log/crontab_lock_problem.$$"
15 # temp files to store stdout and stderr
16 # named with the PID of this script in their name so they'll be unique
17 STDERR="/var/tmp/stderr.$$"
18 STDOUT="/var/tmp/stdout.$$"
20 # $STDOUT and $STDERR are removed when the script exits for any reason
21 trap "rm -f $STDOUT $STDERR" 0
23 # run a command from this script's argument
24 # redirect stdout to $STDOUT file and redirect stderr to $STDERR file
27 echo -n "$DATE: " >> $STDOUT
28 echo "Running command: $@" >> $STDOUT
29 "$@" > $STDOUT 2> $STDERR
31 # get return code from the command run
34 if [ $code != 0 ] ; then
35 # echoing to stdout/stderr makes cron send email
41 # normal exit: just log stdout
43 # lock $LOG with file descriptor 200
45 # if $LOG is locked by other process - wait for 20 sec
46 flock -w 20 200 || LOG=$LOG_LOCK_ERR
47 echo "stdout:" >> $LOG
49 echo "stderr:" >> $LOG