As a developer, I am constantly dealing with the server, and the most annoying thing is that every time I use SSH to connect to the server, I am always worried that the network instability will interrupt the time-consuming tasks on the server side. Sometimes when the server is running commands and wants to check the running status of the server memory and CPU, it is necessary to open another terminal to perform SSH connection operations.

Recently found a good tool: “TMUx”, just can solve these problems, today to share.

First, the benefits of TMUX.

Tmux advantages

  • Split screen

On a Mac, iterm2 can be split. However, if you combine iterm2 with SSH, iterm2 split screen constantly needs to be SSH. As a result, multiple users are connected and the who command is used to check the number of login users.

For example, if iterm2 is divided into three screens, the number of user connections is: 3

However, with TMUx, the number of user connections for the same target is: 1

  • attach

Attach can protect the site so that the working environment will not be lost due to SSH timeout or abnormal exit.

I did a test, first in the TMUx environment git clone code.

Then I shut down the SSH environment for a while. Then I reconnected to the server and found that the working environment remained in state and continued downloading:

The principle I think can be understood as follows: TMUX runs on a remote host and acts as an intermediary. The local terminal should first SSH to the remote host and then run a TMUX session, running tasks in the shell within the session. Before you disconnect the SSH connection locally, end the TMUX session first. Because TMUx runs on a remote host, it can pretend that the SSH connection has never been broken. When the local terminal logs in again, the session can be reloaded and restored to the state before the connection is severed, so as to achieve the goal of uninterrupted download.

In the future, we can safely put time-consuming tasks on tMUX, no longer worry about local SSH disconnection problems!

Install tmux

Under Mac, use BREW directly

brew install tmux
Copy the code

Install tMUx on Centos 7 with yum

yum install tmux -y
Copy the code

But the version will be relatively low, here we can also directly compile the latest version with source code:

Install ncurses-devel and libevent-devel
yum -y install ncurses-devel
yum -y install libevent-devel

# Download source code
git clone https://github.com/tmux/tmux.git

# compiler
cd tmux && sh autogen.sh

./configure && make

# verify versionTmux -v tmux next to 3.1Copy the code

Note:

  • If there is aAclocal: the command was not found

You need to download and install Automake before running autogen.sh

yum install automake -y
Copy the code
  • If there is aMake: yacc: command not found

Install bison:

yum install bison -y
Copy the code
  • ifmakeCan’t install it, you can trymake install

The connection

Add the remote connection key certificate to the ssh-key to facilitate future access.

ssh-add -k key.pem
Copy the code

Then we add a “Profile” to iterm2 and a “Command” to “General” :

ssh -t username@remote_host "tmux attach -t coding01 || tmux new -s coding01"
Copy the code

Tmux attach to coDING01 Session if the remote server already has a COding01 session. If not, a COding01 session is created.

We can see how many sessions there are:

conclusion

With TMUx, we can do a lot more, including tMUX configuration, shortcut key learning and so on, there are people in the market to learn TMUx as art.

Finally, let’s take a look at my TMUX panel. I like to divide it into three panes. The left pane is for regular use, and some real-time or interesting operations (such as displaying ⏲️) can be placed in the upper right corner. Run the top command in the lower right corner to pay attention to the running status of the server.

Use good tools to improve our efficiency.

To be continued

reference

  1. Gitlab.com/gnachman/it…
  2. Blogs.technet.microsoft.com/jessicadeen…
  3. github.com/tmux/tmux
  4. medium.com/@gveloper/u…
  5. Fedoramagazine.org/4-tips-bett…