Rerunning a command in a script that failed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rerunning a command in a script that failed?
# 1  
Old 07-16-2008
Rerunning a command in a script that failed?

I have a script that occasionally has a command here and there that fails and I would like to set my script up to just re run the command if the exit code is 1.

Is there a simple way to do that without if/thens or redirecting to the command again?
# 2  
Old 07-16-2008
How would you know if your script failed or not without testing? you can always manually run it Smilie
# 3  
Old 07-16-2008
You COULD:

until [ $status == 0 ]
do
somecommand someargs
status=$?
done

echo "Exited with status:$status"



but it would loop indefinitely if the command always failed. Would probabally want to put some limit to the number of times to retry.
# 4  
Old 07-17-2008
This is really not a great shell coding best practice but
Code:
somecommand arg1 || somecommand arg1

executes somecommand1 an additional time if it fails the first time.

This will NOT work with pipes i.e., command1 | command2 because the status returned is from the rightmost element in the line, but others may fail. Some implementations have a workaround for this problem.
# 5  
Old 07-18-2008
Quote:
Originally Posted by FunibonE
How would you know if your script failed or not without testing? you can always manually run it Smilie
files will be missing if a specific command fails - I get emailed the files thats how I know. its a reporting script and the apps reporting command sometimes just randomly fails for no reason what so ever... It would be great if there was an easy way to just rerun that one failed command.

I guess I will be a big boy and add in flow control for those commands failing Smilie
# 6  
Old 07-18-2008
Quote:
Originally Posted by Ikon
You COULD:

until [ $status == 0 ]
do
somecommand someargs
status=$?
done

echo "Exited with status:$status"



but it would loop indefinitely if the command always failed. Would probabally want to put some limit to the number of times to retry.
Ill give this a try, Ill need to test it in my environment, this report is kind of heavy on the system so I will defiantly only need to make sure it runs 2x at the most Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Resume from last failed command

Dear Experts, I am creating a shell script (A) which is menu driven and in turn calls another shell scripts (X) depending on the selection. X has a list of commands which runs batch jobs in auto mode. Content of X ./sqr.ksh axhri051_sf axhri051_sf.par $1 $2 XHRPPYOV; ./sqr.ksh axbni062... (19 Replies)
Discussion started by: rprasad
19 Replies

2. Shell Programming and Scripting

How to find whether a particular command has failed inside an sftp script?

hi, how can i know whether a command inside an sftp script has failed or not? i have a sftp expect script #!/usr/bin/expect spawn /usr/bin/sftp abc@ftp.abc.com expect "sftp>" send "cd dir\r" expect "sftp>" send "mput abc.txt\r" expect "sftp>" send "mput def.xls\r" expect "sftp>"... (5 Replies)
Discussion started by: Little
5 Replies

3. Solaris

Script redirect command output failed, why?

Hi, I put a for loop in a script to eject backup tapes from the robot. The command echo' output goes to the log file without problem, but command vmchange's output does not go to the log file although it's working fine. It still displays on the screen. I've tried '2>&1 1>$log', but nothing changed.... (5 Replies)
Discussion started by: aixlover
5 Replies

4. Solaris

Command to find the failed disks in SVM and VxVM

How to find & replace bad disks in solaris & VXVM?plz explain step by step? (2 Replies)
Discussion started by: xtreams
2 Replies

5. Solaris

command 'cc' failed even though gcc is installed

I'm trying to build some python modules on a Solaris 10 machine. It has gcc as /usr/sfw/bin/gcc. # CC=gcc python setup.py build running build running build_py running build_ext cc -c actread.c -o actread.o unable to execute cc: No such file or directory error: command 'cc' failed with exit... (8 Replies)
Discussion started by: aussieos
8 Replies

6. Shell Programming and Scripting

Script exits with $? not 0 randomly, how can I see what command failed?

Hi! I have this situation with 3 shellscripts. One is a "startscript" that simply calls other scripts. This one is scheduled with cron to run at regular intervals. That script runs what I'll refer to as Script 1. Script 1 in turn runs script 2 (import_catalogs_buyer.sh) Sometimes, seemingly... (2 Replies)
Discussion started by: trailsmoke
2 Replies

7. UNIX for Dummies Questions & Answers

make command failed for target 'obj/gp_unix.o'

hi, i am trying to run make command in solaris 9. its giving the error: make:Fatal error:Command failed for target 'obj/gp_unix.o' i came to know X11 should be inastalled for gp_unix.o.But it is already installed.still the same error. ./configure worked fine. can anybody please suggest... (3 Replies)
Discussion started by: rosalina
3 Replies

8. UNIX for Advanced & Expert Users

rm & mv command failed due to too many files.

In our directory there are too many files, & if I try to execute mv *.gz or rm *.l command it fails, providing error string as - 'arg list too long'. This doesnt happen always, is there any way we know, limit on the rm & mv command so we can take care of this failure in future executions ? (9 Replies)
Discussion started by: videsh77
9 Replies

9. UNIX for Dummies Questions & Answers

Problem with Mail command: exec failed. errno=2.

I am trying to send email using the "mail" command. I keep getting an "exec failed. errno=2." message. Here is one the commands I have tried: mail test@mycompany.com < test1.out Any ideas what may be causing this error msg? (1 Reply)
Discussion started by: hawkman2k
1 Replies

10. Shell Programming and Scripting

Failed to get value from a file using sed command

Hi folks, I have the following file (tnsnames.ora): DB10g = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = buffy)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = DB10g) ) ) EXTPROC_CONNECTION_DATA = (DESCRIPTION = (ADDRESS_LIST = ... (2 Replies)
Discussion started by: nir_s
2 Replies
Login or Register to Ask a Question