2017-07-20 22:26:42 +00:00
|
|
|
#!/usr/bin/env bash
|
2016-05-09 20:01:04 +00:00
|
|
|
# /nightlies/cron_wrapper.sh
|
|
|
|
|
|
|
|
# check for no argument case and stop
|
|
|
|
if [ -z $1 ]; then
|
|
|
|
echo "need argument"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# set permanent $LOG file var
|
|
|
|
LOG="/var/log/crontab-nightlies-log/crontab.log"
|
2016-06-06 22:44:06 +00:00
|
|
|
# set $LOG_LOCKED_ERR in case locking failed
|
2016-05-26 20:22:57 +00:00
|
|
|
LOG_LOCK_ERR="/var/log/crontab-nightlies-log/crontab_lock_problem.$$"
|
2016-05-09 20:01:04 +00:00
|
|
|
|
2016-06-06 22:44:06 +00:00
|
|
|
# temp files to store stdout and stderr
|
2016-05-09 20:01:04 +00:00
|
|
|
# named with the PID of this script in their name so they'll be unique
|
|
|
|
STDERR="/var/tmp/stderr.$$"
|
|
|
|
STDOUT="/var/tmp/stdout.$$"
|
|
|
|
|
|
|
|
# $STDOUT and $STDERR are removed when the script exits for any reason
|
|
|
|
trap "rm -f $STDOUT $STDERR" 0
|
|
|
|
|
|
|
|
# run a command from this script's argument
|
|
|
|
# redirect stdout to $STDOUT file and redirect stderr to $STDERR file
|
2016-06-06 22:44:06 +00:00
|
|
|
|
|
|
|
DATE=$(date)
|
|
|
|
echo -n "$DATE: " >> $STDOUT
|
|
|
|
echo "Running command: $@" >> $STDOUT
|
2016-05-09 20:01:04 +00:00
|
|
|
"$@" > $STDOUT 2> $STDERR
|
|
|
|
|
|
|
|
# get return code from the command run
|
|
|
|
code=$?
|
|
|
|
|
|
|
|
if [ $code != 0 ] ; then
|
|
|
|
# echoing to stdout/stderr makes cron send email
|
|
|
|
echo "stdout:"
|
|
|
|
cat $STDOUT
|
|
|
|
echo "stderr:"
|
|
|
|
cat $STDERR
|
|
|
|
else
|
|
|
|
# normal exit: just log stdout
|
2016-05-26 20:22:57 +00:00
|
|
|
|
2016-06-06 22:44:06 +00:00
|
|
|
# lock $LOG with file descriptor 200
|
|
|
|
exec 200>>$LOG
|
|
|
|
# if $LOG is locked by other process - wait for 20 sec
|
|
|
|
flock -w 20 200 || LOG=$LOG_LOCK_ERR
|
2016-08-10 20:15:00 +00:00
|
|
|
echo "stdout:" >> $LOG
|
2016-06-06 22:44:06 +00:00
|
|
|
cat $STDOUT >> $LOG
|
2016-08-10 20:15:00 +00:00
|
|
|
echo "stderr:" >> $LOG
|
|
|
|
cat $STDERR >> $LOG
|
2016-06-06 22:44:06 +00:00
|
|
|
# unlock
|
|
|
|
flock -u 200
|
|
|
|
fi
|