my shell Script stops after doing ssh to remote server
Hi,
Could please anyone help me with my script! My Linux version is uname -a Linux Hostname 2.6.18-164.6.1.el5 #1 SMP Tue Oct 27 11:28:30 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux I have done the keygen part and while logging through script it doesn't ask for any password. But after doing ssh, the scripts just logs into the remote server and not processing further. My script goes likes this..... [START MY_SCRIPT] #!/bin/bash ssh remote-host retVal=$? if [ "$retVal" -eq "0" ]; then cd /apps ; fi echo 'Script Completed' ; [END MY_SCRIPT] Basically it's not able to get into the /apps path. Any help would be greatly appreciated. Thanks, Shasank |
You can investigate why the script behaves as it does by entering the same commands at the command prompt.
|
Well, i did and below are the steps:
1) ssh remote-host 2) cd /apps And its working fine. :) |
What did you do after running ssh remote-host to make it terminate? Did you provide any keyboard input?
|
Well, i did and below are the steps:
1) ssh remote-host 2) cd /apps :) |
Did you provide any keyboard input to terminate the ssh remote-host command?
|
Yes, after logging into remote-host. I use 'exit' command in order to again come back to my local-host.
|
Isn't the problem here that the script is running on the local host, so once you ssh into the remote host it suspends until you return? At least that's what happens when I try this.
Edit: I was wondering how this could be done myself and I came across this, which may be useful: http://stackoverflow.com/questions/3...remote-machine |
Quote:
|
@catkin, nice thought, but still it didn't worked.
[START MY_SCRIPT] #!/bin/bash ssh remote-host retVal=$? if [ "$retVal" -eq "0" ]; then cd /apps ; fi exit ; echo 'Script Completed' ; [END MY_SCRIPT] |
Quote:
Think about it this way... If you were at the terminal, and typed ssh, you would then be connected to the other computer. You could then do whatever you wanted, run whatever you wanted, etc. When you typed "exit", your ssh command would end and it would drop you back to the previous shell. THIS is what your script is waiting for. At this point your script would run its next command, "retVal=$?". Your script will not continue on to the retval, or the cd, etc., until the ssh command has terminated. The ssh command will not terminate until you FINISH whatever it is you are doing on the other machine and return back to the original terminal. Your script is not stopping or getting stuck after running the ssh, it's simply waiting for the ssh to exit so that it can continue running the script. Hopefully that makes sense. |
The return value of "ssh remote-host" is the return code of the last executed command on the remote-host box.
Maybe this is not 0 |
It depends on what you are actually trying to accomplish.
You can execute a script or commands directly i.e. ssh user@someserver 'ls -l;' If your task requires sending commands interactively then you might need to write an expect script. |
@uhelp, it's returning value as 0.
@273, thanks for the wonderful link. It solved my problem. Now, i will be writing two scripts in the local machine. will be having a wrapper script will do the necessary stuff in the remote-host. The link which @273 mentioned had something like this.... ssh root@MachineB 'bash -s' < local_script.sh You shouldn't have to copy the script to the remote server to run it. So, i will have wrapper script which will solve my problem. [START MY_SCRIPT] #!/bin/bash ssh remote-host 'bash -s' < main_script.sh [END MY_SCRIPT] Thanks a lot for all your inputs... Very much appreciated. I thought my problem would take some time to get solved, but amanzingly it got solved within few hours... Thanks again.. :) |
Glad that solved your problem. Please use the link towards the top of the page to mark this thread solved. :)
|
All times are GMT -5. The time now is 07:55 PM. |