Execute Multiple Scripts and Capture Log Details


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute Multiple Scripts and Capture Log Details
# 1  
Old 04-08-2015
Execute Multiple Scripts and Capture Log Details

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

Code:
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
Code:
 
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.

Last edited by Don Cragun; 04-08-2015 at 05:53 PM.. Reason: Please wrap scripts/commands and data inside CodeTags
# 2  
Old 04-08-2015
This statement doesn't look good since you are pushing the scripts into the background with ampersand...

Code:
 "$i" & >> ${LOG_FILE} 2>&1

Plus it looks like you run the scripts again.
I suggest keeping it simple
Code:
#! /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
.
.
.

This User Gave Thanks to blackrageous For This Post:
# 3  
Old 04-08-2015
Note also that you cannot have a space in a variable assignment. To correct the syntax error, change:
Code:
LOG_FILE= FILE.`date ++"%Y%m%d%H:%M:%S"`

to:
Code:
LOG_FILE=FILE.`date ++"%Y%m%d%H:%M:%S"`

Do you really want a + in the file name? If not, change it to:
Code:
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:
Code:
LOG_FILE=FILE.$(date +"%Y%m%d%H:%M:%S")

# 4  
Old 04-08-2015
Could be shorter:
Code:
$ date +%F%T
2015-04-0900:38:18

EDIT:
Oops, there are dashes, oh well, but otherwise... Smilie
This User Gave Thanks to sea For This Post:
# 5  
Old 04-09-2015
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

Code:
#
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...

Last edited by Franklin52; 04-09-2015 at 07:27 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 04-09-2015
You can check the last commands exit status by (for example):
Code:
echo $?

0 means success, whereas everything higher is 'failure'.
So you could do start something like:
Code:
if [ $? -eq 0 ]
then   echo good
else   echo bad
fi

hth
# 7  
Old 04-09-2015
How about
Code:
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.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Need ./ to execute scripts

Hi, I am really sorry for this question but still i am confused. I have shell script called sample.sh I can execute only with the combination of ./sample.sh Is ./ really necessary ? Is it something related with $HOME or $PATH variable. Why and How can i resolve this case ? ... (2 Replies)
Discussion started by: Nandy
2 Replies

2. Shell Programming and Scripting

To execute scripts parallelly

Hi I have set two set of scripts sets in a file which perform similar operations but with different script names for e.g.: 1st set of script1.txt: 1.sh 2.sh 3.sh 4.sh 2nd set of script2.txt: 1_1.sh 2_1.sh 3_3.sh 4_4.sh I want to execute these set of scripts parallelly in such... (16 Replies)
Discussion started by: rohit_shinez
16 Replies

3. Shell Programming and Scripting

Script to execute multiple scripts

Hi All, I have 4 scripts to execute. Each one take anywhere from 3 -9 hours to run depending on what it's doing. I'm running one at a time then check back periodically to see if it's still going and if not, run the other. How can I put these scripts into another script so that they are... (5 Replies)
Discussion started by: bbbngowc
5 Replies

4. Shell Programming and Scripting

Find and execute shell scripts in multiple sub directories in parallel

I have one parent directory and within that parent directory there are several other sub-directories and within those sub-directories there are several other "large number" of sub-directories. All the sub directories have a shell script in them with a common file name execute_command.sh I want... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

5. UNIX and Linux Applications

how to execute multiple .sql scripts from within a shell script using sqlplus

using sqlplus I want to execute a .sql script that has dbms_output statments in rhe script. I want to write the dbms_output statements from .sql file to a log file. is this possible. thanks any help would be appreciated :wall: (1 Reply)
Discussion started by: TRS80
1 Replies

6. Shell Programming and Scripting

Execute multiple SQL scripts from single SQL Plus connection

Hi! I would like to do a single connection to sqlplus and execute some querys. Actually I do for every query one connection to database i.e echo 'select STATUS from v$instance; exit' > $SQL_FILE sqlplus user/pass@sid @$SQL_FILE > $SELECT_RESULT echo 'select VERSION from v$instance;... (6 Replies)
Discussion started by: guif
6 Replies

7. Shell Programming and Scripting

How to capture actual error message when a command fails to execute

I want to capture actual error message in case the commands I use in my shell script fails. For eg: ls -l abc.txt 2>>errorlog.txt In this case I understand the error message is written to the errorlog.txt and I assume its bacause the return code from the command ls -l abc might return 2 if... (3 Replies)
Discussion started by: prathima
3 Replies

8. Shell Programming and Scripting

Need to execute 2 scripts, wait, execute 2 more wait, till end of file

:cool: I need to execute a shell script to do the following: cat a file run two back ground processes using the first two values from the file wait till those background processes finish run two more background processes using the next two values from the file wait till those background... (1 Reply)
Discussion started by: halo98
1 Replies

9. Shell Programming and Scripting

How to execute scripts at logout?

Hi, We can execute scripts at X login time in Linux by placing scripts in /etc/X11/xinit/xinitrc.d/ directory. Can you suggest a method to execute scripts at X logout time? rgds, pcsaji (1 Reply)
Discussion started by: pcsaji
1 Replies

10. UNIX for Dummies Questions & Answers

How can I execute TCL scripts in HP-UX

Hi All, Since I want to execute TCL scripts in HP-UX, I cannot find where to start. I try to execute the following scripts. It gets "tclsh: not found." errors. Where / how to execute tcl scripts? Thanks a lots. #!/bin/sh # the next line restarts using wish \ exec tclsh "$0" "$@" (3 Replies)
Discussion started by: wilsonchan1000
3 Replies
Login or Register to Ask a Question