Script execution in sequence manner


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script execution in sequence manner
# 1  
Old 08-14-2017
Script execution in sequence manner

Hi guys,

I am building a parent script which will call three scripts internally and i would like to run them in sequence manner, i.e. once previous script is successful then only i need to proceed with next else need to fail. I am running in bash mode and flavour is sun
Code:
main_script.sh

sh -x script1.sh > script1.log
status=$?
if [ $status -ne 0 ]; then
echo "Failed due to some reason"
exit 1
fi

sh -x script2.sh > script2.log
status=$?
if [ $status -ne 0 ]; then
echo "Failed due to some reason"
exit 1
fi

sh -x script3.sh > script3.log
status=$?
if [ $status -ne 0 ]; then
echo "Failed due to some reason"
exit 1
fi

I need to better solution to handle this and also i would require if rerun the main_script.sh then the those scripts which are executed successfully shouldn't execute once again. Like for eg. if script2.sh failed and if i run the main script i shouldn't run script1.sh rather than stating the message like script1 already completed hence proceeding with script2.sh

Last edited by Master_Mind; 08-14-2017 at 11:14 PM.. Reason: change in code
# 2  
Old 08-15-2017
Quote:
Originally Posted by Master_Mind
Hi guys,

I am building a parent script which will call three scripts internally and i would like to run them in sequence manner, i.e. once previous script is successful then only i need to proceed with next else need to fail. I am running in bash mode and flavour is sun
Code:
main_script.sh

sh -x script1.sh > script1.log
status=$?
if [ $status -ne 0 ]; then
echo "Failed due to some reason"
exit 1
fi

sh -x script2.sh > script2.log
status=$?
if [ $status -ne 0 ]; then
echo "Failed due to some reason"
exit 1
fi

sh -x script3.sh > script3.log
status=$?
if [ $status -ne 0 ]; then
echo "Failed due to some reason"
exit 1
fi

I need to better solution to handle this and also i would require if rerun the main_script.sh then the those scripts which are executed successfully shouldn't execute once again. Like for eg. if script2.sh failed and if i run the main script i shouldn't run script1.sh rather than stating the message like script1 already completed hence proceeding with script2.sh
There are a lot of pieces here that don't quite seem to fit. I assume that when you say "the flavour is sun" you mean that you are running an old system from Sun Microsystems, Inc. If that is true, then you are running script1.sh, script2.sh, and script3.sh with an old Bourne shell; not with bash.

Assuming that this script is being run by bash or ksh (or some other shell that includes POSIX standard arithmetic substitutions; which does not include /bin/sh on Solaris 10 systems), the following untested code might do what you want:
Code:
[ -f status ] || date '+0 %c' > status

read job datestamp < status

if [ $job -eq 0 ]
then	if sh -x script1.sh > script1.log
	then	job=$((job + 1))
		date "+$job %c" > status
	else	echo "Failed due to some reason"
		exit 1
	fi
fi

if [ $job -eq 1 ]
then	if sh -x script2.sh > script2.log
	then	job=$((job + 1))
		date "+$job %c" > status
	else	echo "Failed due to some reason"
		exit 2
	fi
fi

if sh -x script3.sh > script3.log
then	job=0
	date "+$job %c" > status
else	echo "Failed due to some reason"
	exit 3
fi

although it is totally untested. It assumes that you always run this script in the same directory and uses a file named status in that directory to keep track of the last job that was successfully completed (and when that job last completed successfully).

I hope this gives you some idea of how you could approach this problem and gives you something you can modify to get what you want to work in your environment.
# 3  
Old 08-15-2017
How about
Code:
for i in 1 2 3;
  do    SNAM="script$i"
        if      [ ! -s "$SNAM.log" ]
                  then  if ! sh -x $SNAM.sh &> $SNAM.log 
                          then  mv $SNAM.log $SNAM.err
                                echo "Error in $SNAM.sh"
                                exit $i
                        fi
                  else  echo "$SNAM.log exists - script NOT rerun."
        fi
  done

# 4  
Old 08-16-2017
HI don,

In your case if i rerun the main_script the first script also exuecuting when script2.sh has failed. In my status i can see 1 but since you are using below code its getting replaced
Code:
[ -f status ] || date '+0 %c' > status

# 5  
Old 08-16-2017
It is most simple record the jobs to be skipped, i.e. the good jobs.
Then initially the "good" file can be empty or missing.
Code:
#!/bin/bash
good=./good
for job in script1.sh script2.sh script3.sh
do
  if [ -f $good ] && fgrep -x $job $good >/dev/null
  then
    echo "$job already completed"
  else
    echo "Running $job"
    sh -x $job > ${job%.*}.log
    status=$?
    if [ $status -ne 0 ]; then
      echo "failed with exit $status"
      exit 1
    else
      echo $job >> $good
    fi
  fi
done
echo "all good, removing $good"
rm $good

# 6  
Old 08-16-2017
Thanks all, I corrected the error it worked Don. Thanks all for your suggestion
# 7  
Old 08-16-2017
Quote:
Originally Posted by Master_Mind
HI don,

In your case if i rerun the main_script the first script also exuecuting when script2.sh has failed. In my status i can see 1 but since you are using below code its getting replaced
Code:
[ -f status ] || date '+0 %c' > status

From your post #6 it sounds like you found the problem, but for the record, the code I suggested should work as explained below...

The first time you run that script (or if the file status does not exist), the above statement creates a status file initialized saying that step 0 has been completed. When script1.sh completes successfully, the statements:
Code:
	then	job=$((job + 1))
		date "+$job %c" > status

increment job to 1 and rewrites the status file to contain that 1 and the current date and time stamp.

If script2.sh then fails, the script will exit without further updating the contents of status. The next time you start the script (as long as you haven't removed status and you run it in the same directory), the statement:
Code:
[ -f status ] || date '+0 %c' > status

will find that the file status already exists so it will not update its contents and the following read statement:
Code:
read job datestamp < status

will set job to 1. And since the following test:
Code:
if [ $job -eq 0 ]

will find that 1 is not equal to 0, the following code to run test1.sh will not be run again until script2.sh and script3.sh have both run successfully (or you manually remove the file status or run it in a different directory).
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to sort and compare files in more efficient manner?

Hello All, Iam using below method to sort and compare files. First iam doing sorting and changing the same file and then doing comparing and taking the final result to another file. sort -o temp.txt file1 mv temp.txt file1 sort -o temp.txt file2 mv temp.txt file2 sort -o temp.txt... (6 Replies)
Discussion started by: Vikram_Tanwar12
6 Replies

2. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

3. Programming

Help with make this Fortran code more efficient (in HPC manner)

Hi there, I had run into some fortran code to modify. Obviously, it was written without thinking of high performance computing and not parallelized... Now I would like to make the code "on track" and parallel. After a whole afternoon thinking, I still cannot find where to start. Can any one... (3 Replies)
Discussion started by: P_E_M_Lee
3 Replies

4. Linux

ARM Cortex A9 Core-'1' Execution Sequence

Hi, Iam using OMAP4430 panda board for my project development. It has ARM Cortex A9 MP Core (CORE-'0' & CORE-'1') and iam using linux-2.6.38. Iam just trying to understand the booting sequence for CORE-1. As per my understanding the CORE-1 is triggered from wakeup_secondary(void) in... (1 Reply)
Discussion started by: rajamohan
1 Replies

5. UNIX and Linux Applications

How to log out of the server in such a manner that you are not logged out of PUTTY?

Hi, I am using EXIT command to log out of the server, But this logs me out of the Putty. Is there any way that I can still be present on Putty and just log out of the server. Every time I have to open a new session of PUTTY for a new server (3 Replies)
Discussion started by: himvat
3 Replies

6. Emergency UNIX and Linux Support

Appending the contents of two files in computational manner

Hi, I have two files say file 1 file 2 File1 1 2 4 5 File 2 asdf adf How to get the ouput something like asdf1 adf1 asdf2 adf2 asdf4 adf4 asdf5 (5 Replies)
Discussion started by: ecearund
5 Replies

7. UNIX for Advanced & Expert Users

script with a sequence of nohups

Hi, I have written this script below to run a mathematica notebook, and then I want to launch another one when the first one finishes doing what it must. The notebook includes a command which will exit the program math. If I do it like below the two math processes begin at the same time, and... (3 Replies)
Discussion started by: amen_corner
3 Replies

8. Shell Programming and Scripting

To extract <P> tags in a custom manner from below mentioned input.

Following is input: <P align="justify" ><FONT size="+1" color="#221E1F">the tiny bundles of hairs that protrude from them. Waves in the fluid of the inner ear stimulate the hair cells. Like the rods and cones in the eye, the hair cells convert this physical stimulation into neural im<FONT... (2 Replies)
Discussion started by: parshant_bvcoe
2 Replies

9. UNIX for Dummies Questions & Answers

Script in boot sequence

Hi , I have some problems with my library when the sytem boot : When HPUX is booting, HPUX and STAPE claim the drive initially. HPUX assigns an instance number. The instance number is tied to the hardware path. Near the end of the boot, the ATDD driver claims the drive from STAPE based... (1 Reply)
Discussion started by: delphine
1 Replies
Login or Register to Ask a Question