Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / nightlies / cron_wrapper
1 #!/bin/bash
2 # /nightlies/cron_wrapper.sh
3
4 # check for no argument case and stop
5 if [ -z $1 ]; then
6   echo "need argument"
7   exit 1
8 fi
9
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.$$"
14
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.$$"
19
20 # $STDOUT and $STDERR are removed when the script exits for any reason
21 trap  "rm -f $STDOUT $STDERR" 0
22
23 # run a command from this script's argument
24 # redirect stdout to $STDOUT file and redirect stderr to $STDERR file
25
26 DATE=$(date)
27 echo -n "$DATE: "  >> $STDOUT
28 echo "Running command: $@" >> $STDOUT
29 "$@" > $STDOUT 2> $STDERR
30
31 # get return code from the command run
32 code=$?
33
34 if [ $code != 0 ] ; then
35         # echoing to stdout/stderr makes cron send email
36         echo "stdout:"
37         cat $STDOUT
38         echo "stderr:"
39         cat $STDERR
40 else
41         # normal exit: just log stdout
42
43         # lock $LOG with file descriptor 200
44         exec 200>>$LOG
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
48         cat $STDOUT >> $LOG
49         echo "stderr:" >> $LOG
50         cat $STDERR >> $LOG
51         # unlock
52         flock -u 200
53 fi