Looping in background issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping in background issue
# 1  
Old 10-08-2012
Looping in background issue

Background info :
Linux machines, /bin/bash shell .

I have several remote machines. I want to create a variable for each of these remote machines, by retrieving some data remotely for each of them. As this may take some time for each remote machine, I want to launch these commands woth a for command and to set in background each loop.

Like this :

Code:
for machine in server1 server2
> do
> eval ${machine}_nr=`ssh -q $machine "hostname"`
> done &
[1] 31891

After a second (the ssh commands are already finished) I press an enter and I get this in my prompt (some info regarding my background commands.

Code:
$:
[1]+  Done                    for machine in server1 server2;
do
    eval ${machine}_nr=`ssh -q $machine "hostname"`;
done
$:
$: echo $server1_nr

$:

As you can see the background commands weren't executed, therefore I have no value in the variables server1_nr and server2_nr. The whole chain of commands works fine, otherwise, if I don't send it in background :

Code:
$: for machine in server1 server2
> do
> eval ${machine}_nr=`ssh -q $machine "hostname"`
> done
$:
$: echo $server1_nr
server1.ced.h3g.it

What am I doing wrong ?

---------- Post updated at 08:08 AM ---------- Previous update was at 06:52 AM ----------

A short update :
I think the problem is not so much related with the fact that I'm using a looping statement but with the fact that I cannot create a variable in background (the process of creating the variable itself to be set in background) :
Code:
$: var=78 &
[1] 17155
$:
[1]+  Done                    var=78
$:
$: echo $var

$:

I think it is because if i sent the command "var=78" in background, a separate shell is created, therefore, the variable var will exist only in that shell.
How can I overcame that ?
# 2  
Old 10-09-2012
If var is less than 128 you can use exit status of each child run eg:

Code:
function child_a {
   sleep 5
   return 21
}
 
function child_b {
   sleep 2
   return 17
}
child_a &
r[1]=$!
 
child_b &
r[2]=$!
 
for pid in ${r[@]}
do
   printf "Exist status of $pid is "
   wait $pid
   echo $?
done

# 3  
Old 10-09-2012
Try this test, Put you statements in a script and submit it like below:
Code:
#!/usr/bin/ksh
# Script:  test.sh
for machine in server1 server2
do
  eval ${machine}_nr=`ssh -q $machine "hostname"`
done

nohup test.sh &

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting looping issue

#!bin/ksh --------------------------------------------------------------------------------------------- -- Get sequence number from database --------------------------------------------------------------------------------------------- .os rm... (3 Replies)
Discussion started by: swathi reddy1
3 Replies

2. UNIX for Advanced & Expert Users

Issue with tracking successful completion of Child process running in background

Hello All, I am using Linux. I have two scripts: inner_script.ksh main_wrapper_calling_inner.ksh Below is the code snippet of the main_wrapper_calling_inner.ksh: #!/bin/ksh ppids=() ---> Main array for process ids. fppids=() ---> array to capture failed process ids. pcnt=0 --->... (5 Replies)
Discussion started by: dmukherjee
5 Replies

3. Shell Programming and Scripting

Issue with looping

Hi guys, I am creating a script that checks if a service is UP and running on a bunch of remote servers. My list of servers is: p2 runtime1 8080 p3 runtime2 8080 p4 runtime3 8080 p5 runtime4 8080 p6 runtime5 8080 p7 runtime6 8080 p8 runtime7 8080 p9 runtime8 8080 p10 runtime9 8080... (4 Replies)
Discussion started by: Junaid Subhani
4 Replies

4. Shell Programming and Scripting

Shell scripting issue-running the background script

I have written the below query to genrate a telephone.I am passing account number from oracle database. I am calling 2 scripts which generate the bill 1. bip.sh (it runs in the background) 2.runXitInvoice_PROFORMA_integ bip.sh generates a number which runXitInvoice_PROFORMA_integ uses.How... (7 Replies)
Discussion started by: rafa_fed2
7 Replies

5. Shell Programming and Scripting

Background job issue

How to bring a backgroud job say sample_script.sh to foreground (4 Replies)
Discussion started by: rafa_fed2
4 Replies

6. Shell Programming and Scripting

Odd looping issue with NET::FTP and Proftpd

Hello, I'm a UNIX SysAdmin at a large webhosting company and we have a vendor that provides a service which involves the ftp'ing of files from their servers to ours. They initiate FTP using a perl script with NET::FTP. When they try to transfer files (and delete files over ftp), there is... (3 Replies)
Discussion started by: tmmgeekette
3 Replies

7. Shell Programming and Scripting

[Solved] looping script issue

Hi all.. Need your help I want to combine 2 file into 1file File1 : A B C D File2 : 1 2 3 4 Result : A1 A2 A3 A4 B1 (3 Replies)
Discussion started by: buncit8
3 Replies

8. Shell Programming and Scripting

Looping

Hi evryone i need a help . i have a file xcv.the content is : accelerate i want a script which will run 1000 times in loop and changing the value to accelerate to acceler in 1st loop and in 2nd loop it will be again accelerate and so on . (6 Replies)
Discussion started by: Aditya.Gurgaon
6 Replies

9. Shell Programming and Scripting

Expect Issue Serial Forground Execution vs Concurrent Background Execution

I have an expect script that interrogates several hundred unix servers for both access and directories therein using "ssh user@host ls -l /path". The combination of host/path are unique but the host may be interrogated multiple times if there are multiple paths to test. The expect script is run... (2 Replies)
Discussion started by: twk
2 Replies

10. Shell Programming and Scripting

help with looping

vesselNames values: xxx yyy zzz vesselPlanned values: xxx zzz zzz zzz OIFS="" OIFS=$IFS IFS="\n" (2 Replies)
Discussion started by: finalight
2 Replies
Login or Register to Ask a Question