Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
bash-fu: running X apps on multiple hosts.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
jesnow
l33t
l33t


Joined: 26 Apr 2006
Posts: 856

PostPosted: Sat Sep 12, 2020 8:06 pm    Post subject: bash-fu: running X apps on multiple hosts. Reply with quote

I want to start X apps on several servers at once from the command line. Yes there are tools for this but I just want to use the command line.

I can do:

Code:

jesnow@bartali ~ for word in $(cat /etc/cluster-hosts); do ssh -Y "$word" uptime; done;
 14:51:25 up 4 days, 12:13,  5 users,  load average: 0.03, 0.02, 0.03
 14:53:34 up 111 days,  7:00,  1 user,  load average: 0.00, 0.00, 0.00
 14:52:34 up 6 days, 21:40,  0 users,  load average: 0.00, 0.00, 0.00
 07:46:06 up 4 days, 10:48,  1 user,  load average: 0.00, 0.00, 0.00
 19:54:33 up 14 days,  5:38,  1 user,  load average: 0.00, 0.00, 0.00


This works. I have public key login set up, all hunky dory.

But I want to start X applications on all the servers! It won't do it. It starts each one, then waits for it to exit (eg if I kill it) before starting the next. Ordinarily I would use a '&' to run the job in the background, but I can't figure out how to place the quotations to make it work:

Code:

jesnow@bartali ~ $ for word in $(cat /etc/cluster-hosts); do "ssh -Y $word xosview &"; done;
bash: ssh -Y bartali xosview &: command not found
bash: ssh -Y merckx xosview &: command not found
bash: ssh -Y armstrong xosview &: command not found
bash: ssh -Y craddock xosview &: command not found
bash: ssh -Y coppi xosview &: command not found


Or I can try
Code:

jesnow@bartali ~ $ for word in $(cat /etc/cluster-hosts); do 'ssh -Y $word xosview &'; done;
bash: ssh -Y $word xosview &: command not found
bash: ssh -Y $word xosview &: command not found
bash: ssh -Y $word xosview &: command not found
bash: ssh -Y $word xosview &: command not found
bash: ssh -Y $word xosview &: command not found


Does anybody know how to get the command in the for loop to return without waiting?

Again, I *know* there are tools for this, that's not what I'm asking. I'm asking how to make bash do it.

Cheers,

Jon.
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21623

PostPosted: Sat Sep 12, 2020 8:26 pm    Post subject: Reply with quote

Technically, the first example also waits for the command to exit. You just don't notice it because uptime is so efficient. ;)
Code:
$ for a in 1 2 3; do echo $a & done
[1] 15753
[2] 15754
1
[3] 15755
2
3
[1]   Done                    echo $a
[2]-  Done                    echo $a
[3]+  Done                    echo $a
The key is to use & in place of, rather than in addition to, the ; before the done. Yes, this looks weird.
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3134

PostPosted: Sat Sep 12, 2020 9:05 pm    Post subject: Reply with quote

A protip from man ssh:
Code:
     -f      Requests ssh to go to background just before command execution.
             This is useful if ssh is going to ask for passwords or
             passphrases, but the user wants it in the background.  This im‐
             plies -n.  The recommended way to start X11 programs at a remote
             site is with something like ssh -f host xterm.

             If the ExitOnForwardFailure configuration option is set to “yes”,
             then a client started with -f will wait for all remote port for‐
             wards to be successfully established before placing itself in the
             background.
Back to top
View user's profile Send private message
jesnow
l33t
l33t


Joined: 26 Apr 2006
Posts: 856

PostPosted: Fri Nov 26, 2021 6:45 pm    Post subject: Reply with quote

I'd like to thank everybody who answered. I of course deleted the one-line script this morning and re-create it from the incomplete information that was in this thread. But I got it figured out again and wanted to post my result:

Code:
for word in $(cat /etc/cluster-hosts); do ssh -Yf "$word" xosview & done;


It's not clear to me why the -f does anything, but iput it in there. anddit works.



Cheers,


Jon.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum