Tmux 使用指南
Tmux (Terminal Multiplexer) 是一个强大的终端多路复用器,功能类似于 Screen 但更现代化。它允许你在一个终端窗口中管理多个会话、窗口和窗格。
基础概念
Tmux 有三个层级结构:
- Session (会话): 一组窗口的集合,可以detach/attach
- Window (窗口): 类似于浏览器的标签页
- Pane (窗格): 在一个窗口中分割出的区域
常用命令
会话管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| tmux new -s my_session
tmux new -s training_session
tmux ls
tmux attach -t my_session
tmux a -t my_session
Ctrl+B, D
tmux detach -s my_session
tmux kill-session -t my_session
tmux rename-session -t old_name new_name
|
窗口管理 (在 Tmux 会话内)
所有快捷键都需要先按 前缀键 Ctrl+B,然后再按相应的命令键:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| Ctrl+B, C
Ctrl+B, N
Ctrl+B, P
Ctrl+B, 0-9
Ctrl+B, W
Ctrl+B, ,
Ctrl+B, &
exit
|
窗格管理 (分屏)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| Ctrl+B, "
# 垂直分割窗格(左右分屏) Ctrl+B, %
# 在窗格间切换 Ctrl+B, 方向键(↑↓←→) # 或 Ctrl+B, O # 循环切换
# 关闭当前窗格 Ctrl+B, X # 或 exit
# 显示窗格编号 Ctrl+B, Q
# 调整窗格大小 Ctrl+B, Ctrl+方向键
# 切换窗格布局 Ctrl+B, Space
# 将当前窗格全屏/恢复 Ctrl+B, Z
|
在训练项目中的典型使用场景
场景 1: 长时间训练任务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| tmux new -s robot_training
cd /home/ubuntu/project/robot_train python train.py --config config/BIRL_stable_new_reward.py
Ctrl+B, D
tmux attach -t robot_training
tmux kill-session -t robot_training
|
场景 2: 多任务监控
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| tmux new -s monitoring
python train.py
Ctrl+B, " # 窗格2: 监控GPU watch -n 1 nvidia-smi
# 垂直分割 Ctrl+B, % # 窗格3: 查看日志 tail -f logs/training.log
|
场景 3: 多个实验并行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| tmux new -s experiments
python train.py --config config/BIRL_stable.py Ctrl+B, C
python train.py --config config/BIRL_stable_new_reward.py Ctrl+B, C
./monitor_gpu.sh
Ctrl+B, 0 Ctrl+B, 1 Ctrl+B, 2
|
高级功能
复制模式 (滚动查看历史输出)
1 2 3 4 5 6 7 8 9 10 11
| Ctrl+B, [
Ctrl+B, ]
|
同步窗格 (同时在多个窗格执行相同命令)
1 2 3 4 5 6 7
| Ctrl+B, : setw synchronize-panes on
Ctrl+B, : setw synchronize-panes off
|
保存和恢复会话 (需要插件)
1 2 3 4 5 6 7 8 9 10 11 12
| git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
Ctrl+B, Ctrl+S
Ctrl+B, Ctrl+R
|
配置文件 (~/.tmux.conf)
推荐的基础配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
set -g mouse on
set -g base-index 1 setw -g pane-base-index 1
bind | split-window -h bind - split-window -v
bind h select-pane -L bind j select-pane -D bind k select-pane -U bind l select-pane -R
bind r source-file ~/.tmux.conf \; display "Config reloaded!"
set -g history-limit 10000
set -g status-bg colour235 set -g status-fg colour136
|
应用配置:
1 2
| tmux source-file ~/.tmux.conf
|
Tmux vs Screen 对比
| 特性 |
Tmux |
Screen |
| 窗格分割 |
✅ 原生支持 |
❌ 需要补丁 |
| 配置方式 |
更灵活 |
较简单 |
| 性能 |
更好 |
一般 |
| 社区活跃度 |
🔥 活跃 |
维护中 |
| 学习曲线 |
中等 |
简单 |
快速参考卡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 会话操作: tmux new -s <name> 创建会话 tmux ls 列出会话 tmux a -t <name> 连接会话 Ctrl+B, D 分离会话
窗口操作: Ctrl+B, C 创建窗口 Ctrl+B, N 下一个窗口 Ctrl+B, P 上一个窗口 Ctrl+B, 0-9 切换到指定窗口
窗格操作: Ctrl+B, " 水平分割 Ctrl+B, % 垂直分割 Ctrl+B, 方向键 切换窗格 Ctrl+B, Z 全屏/恢复
其他: Ctrl+B, [ 复制模式/滚动 Ctrl+B, ? 显示所有快捷键
|
实用技巧
- 结合脚本自动化: 创建脚本自动设置工作环境
1 2 3 4 5 6
| #!/bin/bash tmux new-session -d -s dev tmux send-keys -t dev 'cd /home/ubuntu/project/robot_train' C-m tmux split-window -h -t dev tmux send-keys -t dev 'watch -n 1 nvidia-smi' C-m tmux attach -t dev
|
会话持久化: 即使SSH断开,会话依然运行
团队协作: 多人可以同时连接同一个会话(结对编程)
Tmux 是开发和训练深度学习模型的必备工具,特别适合需要长时间运行的任务!