LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-24-2010, 07:51 AM   #1
kirecali
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
script running ssh commands (via loop) exiting after first iteration


Hello everyone,

One of my servers needs to issue an "apt-get update" command on each of the clients in a lab. However, after the first update, it exits. In trying to replicate the problem with a simpler script, I created the following which fetches the listings of multiple directories from a client:

NUM=0
cat paths | while read DIR; do
ssh server-name ls $DIR > /tmp/$NUM.txt
NUM=`expr $NUM + 1`
done

Again, after the first iteration, the script exists. I tried the following line too, but it made no difference:

bash -c ssh server-name ls $DIR > /tmp/$NUM.txt

The only thing that worked so far is this

xterm -e ssh server-name ls $DIR > /tmp/$NUM.txt

Of course, this is far from ideal since a GUI gets involved.

Any thoughts?

Thanks much.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-25-2010, 01:42 AM   #2
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: RHEL/Ubuntu/Debian/Fedora/Centos/K3OS
Posts: 1,159
Blog Entries: 4

Rep: Reputation: 149Reputation: 149
To achieve this :

Quote:
One of my servers needs to issue an "apt-get update" command on each of the clients in a lab.
Try below:

Code:
#!/bin/bash
# Linux/UNIX box with ssh key based login
CLIENTS="192.168.1.1 192.168.1.2 192.168.1.3"
# SSH User name
USR="root"
# connect each client and run apt-get update
for host in $CLIENTS
do
ssh $USR@$host apt-get update
done

And about
Quote:
bash -c ssh server-name ls $DIR > /tmp/$NUM.txt
please elaborate
 
Old 10-25-2010, 04:32 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The problem is that the standard input of your local shell becomes the standard input of the remote command. This is the way ssh works. Every time the remote command terminates, ssh sends a SIGTTIN signal to the local shell, which results in terminating the standard input of the loop. To prevent this behaviour, you can try the -n option of ssh, that explicitly redirects the standard input from /dev/null, leaving the standard input of the local shell untouched.
 
3 members found this post helpful.
Old 10-27-2010, 05:41 AM   #4
kirecali
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
The problem is that the standard input of your local shell becomes the standard input of the remote command. This is the way ssh works. Every time the remote command terminates, ssh sends a SIGTTIN signal to the local shell, which results in terminating the standard input of the loop. To prevent this behaviour, you can try the -n option of ssh, that explicitly redirects the standard input from /dev/null, leaving the standard input of the local shell untouched.
This is exactly what I had needed. Thanks very much.
 
  


Reply



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] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
[SOLVED] bash for loop iteration question. ncalsmitty1369 Programming 4 02-05-2010 05:50 PM
Running multiple commands remotely via SSH in a script gimpy530 Linux - General 4 12-19-2009 10:22 PM
python: can I see which iteration I'm on in a loop? BrianK Programming 2 08-27-2008 09:01 PM
Loop iteration in Linux scripting. 151803 Linux - Newbie 3 03-19-2007 06:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:51 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