Hi All,
I have a requirement to execute multiple scripts (say 4) one after the other in one script and capture log details and error messages in a log file below
LOG_FILE= FILE.`date ++"%Y%m%d%H:%M:%S"`
Script 1 : File_Checkr.sh
Script 2 : Pre_Validation.sh
Script 3 : Testing.sh
Script 4 : Post_Check.sh
I had put all the above 4 scripts in a folder path "SHELL_SCRIPT_PATH" and executing the below in myscript.sh
./myscript.sh
for i in "$SHELL_SCRIPT_PATH/"*
do
"$i" & >> ${LOG_FILE} 2>&1
done
##############FILE Checkr######################
bash $SHELL_SCRIPT_PATH/File_Checkr.sh
if [ $? -eq 0 ] then
echo "File Check Success.............." >> ${LOG_FILE} 2>&1
else echo "File Not Found in $PATH" >> ${LOG_FILE} 2>&1
fi
#############PRE VALIDATION#########################
bash $SHELL_SCRIPT_PATH/Pre_Validation.sh
if [ $? -eq 0 ] then
echo "File Check Success.............." >> ${LOG_FILE} 2>&1
else echo "File Not Found in $PATH" >> ${LOG_FILE} 2>&1
fi
Like this till 4 scripts.
Is there any better way that you can suggest so that the DETAILED logs of each script and error message are captured in log file.
This statement doesn't look good since you are pushing the scripts into the background with ampersand...
"$i" & >> ${LOG_FILE} 2>&1
Plus it looks like you run the scripts again.
I suggest keeping it simple
#! /bin/bash
#script for logging
#
>log.file
echo "Here is script1" >> log.file
script1 >> log.file
echo "Here is script2" >> log.file
script2 >> log.file
echo "Here is script3" >> log.file
script3 >> log.file
.
.
.
1 Like
Note also that you cannot have a space in a variable assignment. To correct the syntax error, change:
LOG_FILE= FILE.`date ++"%Y%m%d%H:%M:%S"`
to:
LOG_FILE=FILE.`date ++"%Y%m%d%H:%M:%S"`
Do you really want a +
in the file name? If not, change it to:
LOG_FILE=FILE.`date +"%Y%m%d%H:%M:%S"`
Ae you really using an old Bourne shell (instead of bash
)? If not, the preferred form of command substitution is:
LOG_FILE=FILE.$(date +"%Y%m%d%H:%M:%S")
sea
April 8, 2015, 6:38pm
5
Could be shorter:
$ date +%F%T
2015-04-0900:38:18
EDIT:
Oops, there are dashes, oh well, but otherwise...
1 Like
Many thanks for the suggestion. I will keep the script looks simple so it will easy to understand for anyone.
However, in your statement below
#
echo "Here is script1" >> log.file
script1 >> log.file
echo "Here is script2" >> log.file
script2 >> log.file
echo "Here is script3" >> log.file
script3 >> log.file
#
Will both the log details and error message can be captured in "log.file". I mean should there be any redirect of stderr to output (2>&1)...
Also there is no condition specified like, if script 1 is success then execute script 2,, so on.. Is this not required in this case...
sea
April 9, 2015, 11:14am
7
You can check the last commands exit status by (for example):
echo $?
0 means success, whereas everything higher is 'failure'.
So you could do start something like:
if [ $? -eq 0 ]
then echo good
else echo bad
fi
hth
RudiC
April 9, 2015, 11:57am
8
How about
script1 || { echo "script1 failed"; exit 1; }
script2 || { echo "script2 failed"; exit 2; }
script3 || { echo "script3 failed"; exit 3; }
script4 || { echo "script4 failed"; exit 4; }
It will run consecutive scripts only if the previous succeeded else print a failure message and quit with the appropriate exit code.
1 Like
Thanks Rudic.
The last one did the help
Is there a way to define any output redirection at starting of my file. instead using LOG_FILE 2>&1 everytime.
somthing like below in script.
exec > LOG_FILE 2>&1
I want all the outputs and error message to get update in LOG_FILE
RudiC
April 13, 2015, 9:16am
11
Well, what be the result of your above redirection? To me, it looks like it should work.
sea
April 14, 2015, 6:42am
12
tui-log -tv "LOGFILE" "MESSAGE"
Will log the MESSAGE to LOGFILE with leading time-stamp and print it (just the MESSAGE) to screen since it is verbose.
So you dont have to pass 2>&1
.
OR something like:
echo "MESSAGE" | tee -v LOGFILE
hth
Thanks Rudic.
Can you help also assist me for a date function.
I mean the the above 4 scripts should run today's business date (Mon-Friday).
If any one the script fails for today for eg, then it should take the next business date and run these 4 scripts again.
I have tried this with the following code
RUN_DATE=`date +%Y%m%d`
NEXT_DATE=`date -d "$d days" +%Y%m%d`
d=1
while (($(date -d "$d days" +%u) >= 6)); do ((++d));
date -d "$d days" +%Y%m%d
<SCRIPT 1>
<SCRIPT 2>
<SCRIPT 3>
<SCRIPT 4>
done
But its running only for necxt date. Can you help me with that please.
RudiC
April 15, 2015, 5:23am
14
I don't understand what you request. Why don't you cron
the four scripts for every business day?