cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Moderator
Moderator

Using tmux to execute commands on servers in parallel

Have you ever needed to run the same command, on multiple servers? I've used cssh and pssh with great success in the past but tmux incorporates the parallelism features, including the features of another old school tool, screen.

I use this tool when teaching RH436L Red Hat High Availability Clustering, as we have to run the same commands on various nodes without using ansible.

Let's install tmux:

yum install -y tmux

Screen Shot 2018-10-25 at 1.04.52 AM.png

Let's run tmux now:

tmux

You should see your terminal window with a horizontal green bar at the bottom:

Screen Shot 2018-10-25 at 1.08.59 AM.png

All tmux commands begin with the prefix ^b (control b) so enter the prefix following by.a command:

I need to monitor the output of top of 4 servers, node[abcd] and look at the output in a single window so what I'm going to do is take my current window and split it horizontally using the command ^b "

Screen Shot 2018-10-25 at 1.13.31 AM.png

We call these elements "panes". Note that my cursor is in the bottom pane.

Now I want to take the pane where my cursor is, and split it vertically using the command ^ b %

Screen Shot 2018-10-25 at 1.16.01 AM.png

My cursor is automatically positioned to be in the bottom right pane.

Next, we want to move the cursor to the top pane, and then split that vertically. To move your cursor around the panes use ^ b o and then use ^ b % to split the top pane vertically.

Screen Shot 2018-10-25 at 1.19.00 AM.png

Now I can use ^ b o to move my cursor around the various panes, and ssh into the nodes I want to run commands on.

To synchronize the commands from one pane, to all the other panes use ^ b :setw synchronize-panes on

You will see the command at the bottom left corner of your tmux window.


Screen Shot 2018-10-25 at 1.30.44 AM.png

Now run your command, I've run top, and it will replicate your keystrokes across all panes.

Screen Shot 2018-10-25 at 1.32.03 AM.png

To turn pane synchronization off, use ^ b :setw synchronize-panes off

Summary of commands (the prefix ^b is implied):

% create a veritcal split

" create a horizontal split

o move cursor between panes

x close a pane

:setw synchronize-panes on turn on pane synchronization

:setw synchronize-panes off turn off pane synchronization

d detach from a tmux session (like ^ a d in screen)

To reattach, use tmux a when you next login.

Tags (2)
3 Replies
Flight Engineer sbulav Flight Engineer
Flight Engineer

Re: Using tmux to execute commands on servers in parallel

Awesome article!

I use tmux every day, and pane syncronization really helped me get through many Red Hat exams.

Small addition from my side - put lines below to your .tmux.conf file:

# Toggle synchronization
bind e setw synchronize-panes on
bind E setw synchronize-panes off

Now press:

<prefix>+e to enable syncronize panes

<prefix>+E to disable

Also, for  anyone interested, consider spending some time reading the  Tao of Tmux

Reply
Loading...
Flight Engineer JayRamsurrun Flight Engineer
Flight Engineer

Re: Using tmux to execute commands on servers in parallel

Thanks for sharing these tips, with zero effort ;-) I can do them!

CTRL + B + " Horizontal
CTRL + B + % Vertical

setw -g mouse-­select­-pane on 
setw -g mouse-­select­-window on 
setw synchronize-panes on 

Scroll in TMUX
ctrl +b + [      (courtesy Mithun from Mauritius for sharing)

0 Kudos
Reply
Loading...
Flight Engineer varelov Flight Engineer
Flight Engineer

Re: Using tmux to execute commands on servers in parallel

Despite what man page of tmux, and this guide says it should work, Ctrl-b-" does not work if I am not into graphical target. I tried to run tmux on CentOS 7.6 Minimal in multiuser target, installs and starts fine except for when I actually try to split screen by Ctrl-b-", it waits for me to input some kind of index, and fails to acknowledge as a valid index anything I put in.

HEAVY EDIT: It wants you to press Ctrl-b, release, and then " (or other commands)... now that I found that detail out, it works (after glancing at somebody else's blog on tmux).

0 Kudos
Reply
Loading...
Join the conversation
You must log in to join this conversation.