3

Linux 终端复用器 tmux 提高通过 SSH 运行的程序稳定性

 1 year ago
source link: https://xujinzh.github.io/2023/03/01/linux-tmux-ssh/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Linux 终端复用器 tmux 提高通过 SSH 运行的程序稳定性

2023-03-01technologylinux

3 2.8k 3 分钟

在使用 SSH 远程连接服务器后,在终端上运行长时间占用终端的程序(如 pip install torch==1.10.1+cu111 torchvision==0.11.2+cu111 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu113/torch_stable.html 等大文件下载)容易受到网络中断等情况导致的 SSH 连接断开的影响。这是因为 SSH 终端窗口和会话是绑定的,当终端窗口断开时,会话也关闭,会话中的进程也随之关闭。使用 tmux 可以将会话与终端窗口“解绑”。这样,终端窗口关闭或异常断开时,不影响当前会话中运行的程序。

tmux 是一个终端复用器(terminal multiplexer),可以将会话与窗口“解绑”,窗口关闭时,会话不会终止,而是继续在后台运行,当重新把会话绑定到其他窗口时,里面运行的进程还在。tmux 有如下特点:

  1. 运行在单个窗口中同时访问多个会话;
  2. 新窗口可以接入已经存在的会话;
  3. 运行一个会话同时连接到多个窗口,实现多用户实时共享会话;
  4. 支持窗口在垂直和水平方向上任意拆分。

之前介绍过 screen 命令,也是一个终端复用器,但是没有 tmux 强大。

sudo apt update
sudo apt install tmux

常用会话操作

# 启动一个会话并命名为 remote,同时会打开一个新窗口
tmux new -s remote

# 在当前会话中运行命令行程序,例如
pip install torch==1.10.1+cu111 torchvision==0.11.2+cu111 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu113/torch_stable.html

# 程序运行时,也可退出当前窗口,使会话和窗口解绑。注意不是退出会话
# 快捷键 ctrl + b, d

# 重新绑定会话到一个新窗口
tmux attach -t remote

tmux 窗口有很多快捷键。所有快捷键都是通过前缀键唤醒,默认前缀键是 ctrl + b,即当先按下 ctrl + b 时才启动快捷键。如 ctrl + b 后再按 d 表示退出当前窗口,会话以后台方式运行。ctrl + b 后再按 ? 表示唤醒帮助信息,退出帮助按 ESC 或 q 键。

# 默认情况下,直接创建会话以序号命名,从0开始
tmux

# 新建一个具有自定义名称 remote 的会话
tmux new -s remote

当窗口上没有运行程序时,可以使用如下命令:

tmux detach

当窗口上运行程序时,使用快捷键:ctrl + b, d

tmux list-session
# 或者
tmux ls
# 绑定会话名为 remote 的会话
tmux attach -t remote
# 或者
tmux attach-session -t remote

# 如果会话名是序号,也可以,比如绑定会话 0
tmux attach -t 0

当当前窗口绑定到该会话时

exit
# 或者使用快捷键 ctrl + d

当要退出的会话 remote 或 0 没有绑定到当前窗口时

tmux kill-session -t remote
tmux kill-session -t 0
# 切换到会话 0
tmux switch -t 0

# 切换到会话 remote
tmux switch -t remote
# 把会话 0 改名为 remote
tmux rename-session -t 0 remote

常用快捷键

当打开某个会话时,可以使用如下快捷键:

  • ctrl + b, d: 分离当前会话
  • ctrl + b, s: 列出所有会话
  • ctrl + b, $: 为当前会话改名
tmux new-window

# 指定名称
tmux new-window -n newwin
# 切换到编号 0 的窗口
tmux select-window -t 0

# 切换到名称为 newwin 的窗口
tmux select-window -t newwin
# 把当前窗口重命名为 newwin2
tmux rename-window newwin2

窗口快捷键

ctrl + b, c:创建一个新窗口,状态栏会显示多个窗口的信息。
ctrl + b, p:切换到上一个窗口(按照状态栏上的顺序)。
ctrl + b, n:切换到下一个窗口。
ctrl + b, :切换到指定编号的窗口,其中的是状态栏上的窗口编号。
ctrl + b, w:从列表中选择窗口。
ctrl + b, ,:窗口重命名。

tmux 可以将当前窗口划分为多个窗格(pane),可以在每个窗格运行不同的命令。

以下操作都是在当前窗口绑定到某个 tmux 会话下操作的。

# 上下分割窗格
tmux split-window

# 左右分割窗格
tmux split-window -h

光标在窗格间切换

虽然一个窗口下可以分割出多个窗格,但是鼠标切换不能简单的通过左键完成,需要使用命令完成

# 光标移动到上方窗格
tmux select-pane -U

# 光标移动到下方窗格
tmux select-pane -D

# 光标移动到左边窗格
tmux select-pane -L

# 光标移动到右边窗格
tmux select-pane -R

交换窗格位置

# 窗格上移
tmux swap-pane -U

# 窗格下移
tmux swap-pane -D

更多窗格快捷键

ctrl + b, %:划分左右两个窗格。
ctrl + b, “:划分上下两个窗格。
ctrl + b, :光标切换到其他窗格。是指向要切换到的窗格的方向键,比如切换到下方窗格,就按方向键↓。
ctrl + b, ;:光标切换到上一个窗格。
ctrl + b, o:光标切换到下一个窗格。
ctrl + b, {:当前窗格与上一个窗格交换位置。
ctrl + b, }:当前窗格与下一个窗格交换位置。
ctrl + b, ctrl + o:所有窗格向前移动一个位置,第一个窗格变成最后一个窗格。
ctrl + b, alt + o:所有窗格向后移动一个位置,最后一个窗格变成第一个窗格。
ctrl + b, x:关闭当前窗格。
ctrl + b, !:将当前窗格拆分为一个独立窗口。
ctrl + b, z:当前窗格全屏显示,再使用一次会变回原来大小。
ctrl + b, ctrl + :按箭头方向调整窗格大小。
ctrl + b, q:显示窗格编号。

# 列出所有快捷键,及其对应的 tmux 命令
tmux list-keys

# 列出所有 tmux 命令及其参数
tmux list-commands

# 列出当前所有 tmux 会话的信息
tmux info

# 重新加载当前的 tmux 配置
tmux source-file ~/.tmux.conf

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK