Automatically Rerun a script based on previous execution output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automatically Rerun a script based on previous execution output
# 1  
Old 06-25-2009
Automatically Rerun a script based on previous execution output

Hi Experts,

I have a shell script called "updatevs" that is scheduled to run at 6.00 am everyday via cronjob. The cronjob will execute this script and output to a log file. The functionality of this script is to read the database and run a set of commands. This script is generally successful but some times due to network outage or Database being down it fails.

Only in case of a failure i would like to rerun the script every 20 mins for the next 2 hours, if still not successful after 2 hours i would like an email to be triggered to address "xxxxx@abc.com"

There may be several ways to solve this case however I was thinking of writing a small script called "checklog" which reads the log file and searching for the obevious words like "ERROR", when found "Checklog" script will rerun "updatevs" script. I don't think this is the solution as i can't see a way of making this recurring.

Please let me know your valuable feeback on how to acheive this.

Many Thanks
# 2  
Old 06-25-2009
Rather than doing a search in the log files to check if the script updatevs completed successfully or not, I think a better way to do accomplish would be to do the following:
1. Modify the script updatevs to exit with a non-zero return code on failures.
2. Write another wrapper script that would call updatevs and based on the return code of the script decide whether it needs to be called again..

Just a sample of how the wrapper would look like (Not tested):
Code:
#New Job for updatevs
typeset -i exitStatus=1
typeset -i retryCntr=0
function callUpdateEnv
{
  . updatevs
  return $?
}

while [ $exitStatus != 0 ]
do
   
   callUpdateEnv
   exitStatus=$?
   if (( $exitStatus != 0 )); 
   then 
      (( retryCntr = ${retryCntr} + 1 ))
      if (( $retryCntr > 5 ));
         mailx -s "Job failed" xxxxx@abc.com < /dev/null
      fi
      sleep 1200
   fi
   
done

echo "completed successfully"

There would be several other better ways to do it but going through the logs doesn't seem to be the right one...

regards,
Arun.
# 3  
Old 06-25-2009
Quote:
Originally Posted by forumthreads
I have a shell script called "updatevs" that is scheduled to run at 6.00 am everyday via cronjob. The cronjob will execute this script and output to a log file. The functionality of this script is to read the database and run a set of commands. This script is generally successful but some times due to network outage or Database being down it fails.

Only in case of a failure i would like to rerun the script every 20 mins for the next 2 hours, if still not successful after 2 hours i would like an email to be triggered to address "xxxxx@abc.com"

There may be several ways to solve this case however I was thinking of writing a small script called "checklog" which reads the log file and searching for the obevious words like "ERROR", when found "Checklog" script will rerun "updatevs" script. I don't think this is the solution as i can't see a way of making this recurring.

Code:
n=0
while ! updatevs
do
  [ $n -le 6 ] && sleep 1200
  if [ $n -gt 6 ]
  then
     echo whatever | mail -s "updatevs failed" xxxxx@abc.com
     exit 1
  fi
  n=$(( $n + 1 ))
done

# 4  
Old 06-26-2009
Hi Arun,

Many thanks for your solution.

This is what i have done to script updatevs to exit with a non-zero code on failures, please suggest if this is correct or not

###################
if [$? -ne "0"]
exit
###################

These lines are added after the last command executed in script "updatevs"

I created a new file called wrapper and have got your code in it but when i run wrapper is reports the following error

syntax error at line 19: 'fi' unexpected

Any suggestions appreciated.

Many thanks
# 5  
Old 06-26-2009
In your script updatevs, I would say add exit $? at the end of the script. That way, the wrapper would be able to trap the correct return code.

The wrapper was failing because of a missing then

Code:
Instead of:
   if (( $retryCntr > 5 ));
Use:
   if (( $retryCntr > 5 )); then

regards,
Arun.
# 6  
Old 06-30-2009
Arun,

Thanks again. The wrapper script seems to work with out any error messages however it does not produce any email. Here is what i did:

changed the sleep value to 30 in Wrapper so i need not have to wait long to see the eamil.
Ran wrapper.

wrapper seems to execute updatevs script successfully but i was expecting wrapper script to display the message "completed successfully".

commented sleep in wrapper, I modified "updatevs" script to ensure it fails hoping the wrapper to send an email but wrapper does not send any mail.

The mailx command does send out an email when this command is run on its own.

Please can you see where i am going wrong.
# 7  
Old 07-06-2009
Can you please paste here the code for both the scripts that you are using? Would be much more easier for me to find what the issue is...

regards,
Arun.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a script before and after reboot automatically and send output to two locations.

Hello Team . I am working a health check script ( bash) to run on linux server ( RedHat) and requirements are 1. The o/p of script need to be send to two diff files . I am testing with tee command . But I am not successful yet , any recommendations if that is the right approach ? 2. The same... (2 Replies)
Discussion started by: Varja
2 Replies

2. UNIX for Advanced & Expert Users

Graphical Display Of Script Execution & Output

Hello All i have a KSH script which basically takes attribute name as input argument and searches whole Netezza appliance and prints information of where that column is used (Table/Views) etc. Problem with this approach business users have to raise SUDO access request, Install Putty, run through... (1 Reply)
Discussion started by: Ariean
1 Replies

3. Shell Programming and Scripting

Compressing previous log automatically

I want to create a script that will zip the previous log. Example. abc.log.2012.12.02 abc.log.2012.12.01.gzip abc.log If today is 2012.12.03 , my current log is abc.log and my previous date is 2012.12.02, i want abc.log.2012.12.02 to compress everytime I run the script. I can... (5 Replies)
Discussion started by: kaibiganmi
5 Replies

4. Shell Programming and Scripting

output from remote server during execution of a script

How to see the output from remote server during execution of a script ? I am executing a script (ls) from machine 1 but the o/p should be displayed in machine 2. Can I achieve this ? Example:- Machine 1:- # ls Machine 2:- (console) file1 file2 file 3 dir1 dir2 (0 Replies)
Discussion started by: frintocf
0 Replies

5. Emergency UNIX and Linux Support

invoke one script based on previous script execution

I am database guy and not very good at shell scripts. I am seeking help to sharp my script coding. I have 5 scripts 1. master script. I use this one to call other four scripts to do database work. 2. db_backup_1 and log_backup_1 3. db_backup_2 and log_backup_2 in master script, I want to... (4 Replies)
Discussion started by: duke0001
4 Replies

6. Shell Programming and Scripting

Improve script made to calculate value based on present and previous line

Hi all, I have made at small script to make a simple calculation on a file which is formatted in this way: I want to create a new file in which the value of particular line minus the previous line is printed. So my wanted output is: I have made the following program to do the job... (5 Replies)
Discussion started by: s052866
5 Replies

7. Shell Programming and Scripting

Awk script to create new file based on previous line

Need help creating a script that does the following: Sort a file Compare the previous line "last field" with current line "last field" If they are the same, print output to a file If they are different print output to a new file The script should keep creating new files if the previous... (5 Replies)
Discussion started by: Muga801
5 Replies

8. Shell Programming and Scripting

Execution Output of a shell script into a file.

Hi Experts, I have a script called test.sh. I am trying to execute it with sh -x test.sh. Where i can find sequence of steps executed one by one. Now i want to these executions to be captured in a file. i.e sh -x test.sh > output.txt the above one is notworking. can anyone help me... (6 Replies)
Discussion started by: naree
6 Replies

9. Shell Programming and Scripting

Script - How to automatically start another process when the previous process ends?

Hi all, I'm doing automation task for my team and I just started to learn unix scripting so please shed some light on how to do this: 1) I have 2 sets of datafiles - datafile A and B. These datafiles must be loaded subsequently and cannot be loaded concurrently. 2) So I loaded datafile A... (10 Replies)
Discussion started by: luna_soleil
10 Replies

10. UNIX for Advanced & Expert Users

rerun a script if aborted and exited

Hello, When I run a script, the script sometimes exited. I need to re-run it automatically again. Currently I am using crontab to run the script each 5 minutes. However, I need to have a better solution. Any idea what to do? thanks (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question