LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2017, 05:42 PM   #1
pgb205
Member
 
Registered: Nov 2007
Posts: 129

Rep: Reputation: 15
Executing on remote server terminates while loop


Bash script looks like

#!/bin/bash
local_command1
while read line; do
local_command2;
ssh remotesrv "somecommand";
done< /tmp/file

/tmp/file has multiple lines but if I run as above the script stops after reading only first line. Commenting out ssh remotesrv "somecommand"; makes while loop read entire file as intended. I suspect it's something with having to connect to remote shell that breaks the loop but am not sure.

Please let me know what I am doing incorrectly.
 
Old 04-07-2017, 08:04 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,669

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
Does the script stop and you are back and the command line prompt or does your script just hang?

Can you connect to the remote server using ssh from the command line?

Are you using ssh keys?
 
Old 04-07-2017, 09:52 PM   #3
pgb205
Member
 
Registered: Nov 2007
Posts: 129

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by michaelk View Post
Does the script stop and you are back and the command line prompt or does your script just hang?

Can you connect to the remote server using ssh from the command line?

Are you using ssh keys?
I'm back at the command prompt on the server where the above script was run from. Yes I can connect to the remote server via ssh using keys.
 
Old 04-07-2017, 10:45 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
(Please use [code][/code] tags.)

The reason your script stops is because the ssh command reads from stdin, which means it reads all the rest of your /tmp/file on the first run. You can solve this by redirecting or using the -n option.
Code:
ssh remotesrv "somecommand" < /dev/null
# or
ssh -n remotesrv "somecommand"
ssh(1)

Code:
     -n      Redirects stdin from /dev/null (actually, prevents reading from
             stdin).  This must be used when ssh is run in the background.  A
             common trick is to use this to run X11 programs on a remote
             machine.  For example, ssh -n shadows.cs.hut.fi emacs & will start
             an emacs on shadows.cs.hut.fi, and the X11 connection will be
             automatically forwarded over an encrypted channel.  The ssh program
             will be put in the background.  (This does not work if ssh needs to
             ask for a password or passphrase; see also the -f option.)
 
1 members found this post helpful.
Old 04-07-2017, 11:01 PM   #5
pgb205
Member
 
Registered: Nov 2007
Posts: 129

Original Poster
Rep: Reputation: 15
ntubski . thank YOU! I think this helped.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] why , this for loop is executing infinite times tushar_pandey Programming 15 08-22-2012 06:18 AM
Trouble executing a .sh file on centos 6.1 x64 on a remote linux server. cianz182 Linux - Newbie 8 08-12-2012 04:44 PM
[SOLVED] >while read< loop terminates/quits after a single cycle paziulek Linux - General 3 07-14-2011 08:52 AM
How do I keep an executable from terminates after the initiating shell terminates mr.v. Linux - Newbie 8 01-20-2007 02:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration