ssh can't back from remote host during script execution

Hi all

I wrote a script to execute a script on several remote hosts, but somehow during the execution of the local script, ssh can't come back from the remote host, so that causes my local script hanging... I use the below command to do the job in the local script, any idea?

ssh <user>@<remote host> "<path name>/<script name>" >> WorkLog.log

I think it may have some relation with the tunnel not closed, but I don't know how to solve that...

Try to put the script which you want to use in some remote host under the below syntax :

"connection string" !<<EOF

your code

!

This helps the remote host to identify the part of the script its executing remotely.
! you can keep anything but consistency nneds to be maintaned for both the places.

Thanks.

Thanks navojit, will try your method.

May be the script is taking too long to execute or ssh may be the problem, try using ssh -vv to debug if above technique does not work.

Hi dinjo, the script is very short actually, by monitoring the log I found the remote script can finish, but can't come back from remote machine.

Did you tried running the script from command line.

Hi dinjo, that script on that remote host execute a series of other scripts. I can briefly tell you what the remote script do,it run a stop script->show status-> run a start script -> show status. After the second show status done, it will just be hanging there.

If I ssh to that host and run scripts, they runs ok

ssh hostname
cd <path name>
hostname>./stop script
hostname>./show status
hostname>./start script
hostname>./show status

If I remote execute in my local script

ssh <user>@<remote host> "<path name>/<stop script>" >> WorkLog.log
ssh <user>@<remote host> "<path name>/<show status>" >> WorkLog.log
ssh <user>@<remote host> "<path name>/<start script>" >> WorkLog.log
ssh <user>@<remote host> "<path name>/<show status>" >> WorkLog.log

every thing I need will be in log, that means they are executed, but I need to use Ctrl+C to get my prompt back on local machine.

Attach a nohup to the script and run it.

Ye, dinjo, I agree, I will also try this when I got chance to test.... Thank you so much!!! :slight_smile:

Can you try to run the piece of code like below :

"connection string" ! << >> WorkLog.log

your code

!

Thanks.

Sure navojit I will test this weekend, as the system is in serious use now.

Try Net::SSH::Perl

Hi all, I pretty much resolved this, the cause of this problem is:

SSH connects stdin, stdout and stderr of the remote shell to your local terminal, so you can interact with the command that's running on the remote side.

As a side effect, it will keep running until these connections have been closed, which happens only when the remote command and all its children (!) have terminated (because the children, which is what "&" starts, inherit std* from their parent process and keep it open).

So I modify my code to wrap those 4 cmds in script and run it like this

ssh <user>@<host> "<wrapped_script> >& Worklog.log"

which redirect stdout and stderr to a file, I am using csh. Thank you for your help.