Multiplexing and Making Terminal Sessions Persistent with tmux

  1. You want to see how your web server responds to stress tests. You need to log in through SSH two times: in a window you open a resource monitoring application like “htop” and in another window you enter commands to perform the stress test. With a terminal multiplexer you can avoid multiple logins, launching as many shell sessions as you need, in a single SSH session. Here’s a picture of three Bash shells hosted in tmux:
  1. You need to compile a large project. It is estimated that this job will require at least 8 hours. If your SSH connection drops unexpectedly after say, 4 hours, the job will be interrupted and you may have to restart from scratch. If you run the compile in a tmux session, it doesn’t get interrupted even if your connection drops or you simply need to log out from the server. You can later come back, reattach to the session and see how the compile job has progressed during your absence.
  2. You are running a game server which doesn’t have a ready-made daemon script to put the process in the background. Instead, this program runs in the foreground and if you would quit your SSH session, it would be closed. You do have options to daemonize a process yourself, either through Bash commands or by writing systemd scripts, but, in this case, it’s unfeasible because the game server utility also provides its own command line. Losing the terminal session would leave you without the ability to further interact with the program’s input and change game settings while it’s running. With tmux, you can detach from the terminal, let the game utility run, and when you log back in you can re-attach to the terminal and input commands to kick or ban users, change maps and manage the game hosted on your instance.
  1. Have to run multiple terminal sessions in a single SSH session.
  2. Run jobs that take a very long time to complete and we can’t risk getting disconnected and losing progress.
  3. Have a daemon type process that runs permanently on the instance and we need the ability to detach and reattach, as needed, to the terminal hosting the application to reestablish connectivity to the utility’s text-based input/output interface.

Install tmux

apt update && apt install tmux
yum install tmux
zypper refresh && zypper install tmux

Attach and Detach from tmux Sessions

tmux
for i in `seq 1 $((10*60)) | tac`; do clear; echo $i; sleep 1; done; echo "Finished!"
tmux attach
tmux attach

Manage tmux Windows

exit

tmux Panes

tmux Command Mode

new-session
echo 'set -g mouse on' >> ~/.tmux.conf

List of tmux Shortcut Keys

  1. c — Create a new window.
  2. n — Switch to the next window.
  3. p — Switch to the previous window.
  4. 0 to 9 — Select window by its index number.
  5. w — Display an interactive menu with all available windows. You can navigate this menu with the arrow keys and switch to the desired window.
  6. & — Kill current window; forcibly terminates all processes running in all the panes currently displayed.
  1. % — Split in two vertical panes.
  2. “ — Split in two horizontal panes.
  3. o — Switch to the next pane available in the current window.
  4. { or } — Swaps/moves the currently selected pane to the position of the next pane, or the previous one.
  5. ARROW KEY — Any of the four arrow keys (left, right, up, down), will select the pane adjacent to the respective direction, relative to the currently active pane.
  6. CTRL+ARROW KEY — Resize the currently active pane in the direction of the arrow key. Pressing the same arrow key multiple times (without releasing CTRL), resizes the pane further. In case you’re having problems with this in PuTTY, read: https://superuser.com/questions/1165287/ctrl-arrow-keys-for-tmux-not-working-in-putty
  7. x — Kill (force close) all processes running in current pane and close it.
  8. z — Zoom into pane (maximize). Press combination of keys again (CTRL+B, z) to zoom out.
  1. ( or ) — Switch to the previous or next session.
  2. s — Display an interactive menu with available sessions. You can navigate the menu with your arrow keys and press enter to switch to the desired session.
  3. $ — Rename the currently active session.

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Debugging Remotely with PyCharm: Step to Step Tutorial

CS371p Spring 2022: David Klingler

LINQ — Language Integrated Query

Identify the design pattern in Drupal

Dependency Injection Design Pattern — Drupal 8

piplanning app Implementation Roadmap

4 Really Small Things That Say a Lot about You

Deploy Django (w/ Django Rest Framework) and Connect to Database using AWS Elastic Beanstalk

Using Capybara Effectively with 8 Useful Configuration Parameters

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Demystifying the Buildpacks frontend for BuildKit

Benchmarking BRGM’s EFISPEC3D earthquake simulation application on AWS

Just-in-Time Nomad: Templating HashiCorp Nomad Jobs with Nomad Packs

License Compliance and Security with Open Source Components

balancing on a rope high above rocks near the ocean