Shell Script to Retry and Exit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to Retry and Exit
# 1  
Old 11-18-2010
Shell Script to Retry and Exit

ok, so I'm trying to add a function to my local script that runs a command on a remote host. The reason why this is needed is that, there are other scripts that run different commands on the same remote host.

so the problem is that many times there are multiple scripts being run on the remote host, which then causes collisions and all the multiple scripts abort with a "Query Timed Out" message. All these scripts connect to port 1040 on the remote host...which is why i guess they time out.

i want to put something in my local script try to run to a command on the remote host three times. during those three times, if it doesn't run into any problems, the script should just break.

if anyone has a better idea than the below please let me know.

thanks

Code:
#!/bin/sh

x=3

while [ $x -le 4 ] ; do

response=`echo "external_cmd backup.sh" | nc -w 3 bogus.godaddy.com 1040`

if [ $? -eq 0 ] ; then

break

elif [ $? -ne 0 ] ; then

response=`echo "external_cmd backup.sh" | nc -w 3 bogus.godaddy.com 1040`

fi

done

this is a nagios script by the way.
# 2  
Old 11-18-2010
If you need to run the script with the old Bourne shell:

Code:
counter=0 limit=3
while [ "$counter" -lt "$limit" ]; do
  response=`echo "external_cmd backup.sh" | nc -w 3 bogus.godaddy.com 1040` &&
    break
  counter=`expr $counter + 1`
done

With POSIX compliant shell you could use something more efficient:

Code:
counter=0 limit=3
while [ "$counter" -lt "$limit" ]; do
  response="$( 
    echo "external_cmd backup.sh" | 
      nc -w 3 bogus.godaddy.com 1040 
      )" &&
    break
  counter="$(( $counter + 1 ))"
done

zsh and recent versions of ksh93 and bash support a modern syntax like this:

Code:
counter=0 limit=3
while (( (( counter++ )) < $limit )); do
  response="$( 
    nc -w 3 bogus.godaddy.com 1040 <<< 'external_cmd backup.sh' 
    )" &&
    break
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read exit value from Shell script

Hi all, I have a situation to read exit value of a command (not exit code) and process further. bash-4.2$ returnvalue=`ssh DOMAIN\\\\user1@10.7.7.68 'cmd /c "del C:\Users\user1\db_test.bak"'` Could Not Find C:\Users\user1\db_test.bak bash-4.2$ echo $? 0 bash-4.2$ echo $returnvalue... (3 Replies)
Discussion started by: baluchen
3 Replies

2. Shell Programming and Scripting

Exit the shell script

Hi, suppose my script is sample.sh i have to run using '. ./sample.sh' as . ./script file always executes the script in my parent shell. when my sample.sh contains exit command .. my environment is getting closed as am executing in the parent shell ... please suggest me how can i use... (5 Replies)
Discussion started by: pracheth
5 Replies

3. Shell Programming and Scripting

How to capture the exit code of a shell script in a perl script.?

hi, i want to pop up an alert box using perl script. my requirement is. i am using a html page which calls a perl script. this perl script calls a shell script.. after the shell script ends its execution, i am using exit 0 to terminate the shell script successfully and exit 1 to terminate the... (3 Replies)
Discussion started by: Little
3 Replies

4. Shell Programming and Scripting

Exit a shell script

Hi Guys, How can I exit from the shell script if a condition is not met? Thanks in advance... (2 Replies)
Discussion started by: Phuti
2 Replies

5. Shell Programming and Scripting

exit shell from a script

hi guys I have a script that I need to terminate or exit the shell or session completely for the user but the exit only exit from the script and takes the user to the shell I found this https://www.unix.com/unix-dummies-questions-answers/399-using-exit-command-shell-script.html saying that... (1 Reply)
Discussion started by: kopper
1 Replies

6. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

7. Shell Programming and Scripting

how to exit out of the calling Shell Script

Hi All, I tried looking for this, but was not able to get a clear answer. Hence posting here. Please find the details below. Thanks for the help I have 2 shell scripts, script1.sh and script2.sh. I call script2.sh from within script1.sh ( by simple ./script2.sh command). Based on some... (7 Replies)
Discussion started by: harimac
7 Replies

8. Shell Programming and Scripting

unable to exit from a shell script

Hi All, I am unable to exit from a shell script using the below code: #!/bin/ksh passchk() ( if ;then echo "Password validated" else echo "Wrong password Quiting the application..." exit 0#not working fi ) passchk (Note:"finalresult" passed to the passchk... (2 Replies)
Discussion started by: Sreejith_VK
2 Replies

9. Shell Programming and Scripting

exit a shell script!!

could somebody tell me please how to exit a shell script: if then echo "No arguments detected" exit 1 fi ... echo "still there" # is displayed .. :-( (4 Replies)
Discussion started by: sami98
4 Replies

10. UNIX for Dummies Questions & Answers

using exit command in a shell script

Can it be done? If so, how? I would like a script to contain the exit command, and log me off at script completion. thanks (1 Reply)
Discussion started by: jpprial
1 Replies
Login or Register to Ask a Question