使用 tmux 在远程服务器上管理深度学习训练会话
目录
概述
在远程服务器上进行深度学习训练时,经常需要长时间运行任务。如果直接通过SSH运行训练脚本,当网络断开时,训练进程会被终止,导致前功尽弃。tmux 是一个终端复用器,可以创建持久化的会话,让你安全地断开连接,稍后再重新连接继续工作。
安装 tmux
Ubuntu/Debian
sudo apt-get update
sudo apt-get install tmux
CentOS/RHEL
sudo yum install tmux
macOS
brew install tmux
完整工作流程示例
1. SSH 连接到远程服务器
ssh username@your_server_ip
2. 创建 tmux 会话
创建一个名为 "lora_training" 的新会话:
tmux new -s lora_training
参数说明:
-
new: 创建新会话 -
-s lora_training: 指定会话名称(可自定义)
3. 在 tmux 会话中工作
进入 tmux 会话后,你可以看到一个新的终端界面,底部状态栏会显示会话名称。
激活 conda 环境
# 查看可用环境
conda env list
# 激活你的环境
conda activate your_env_name
进入项目目录
# 使用绝对路径
cd /new_disk/lmx/.project/T2I_lora_moe
# 或者相对路径
cd ~/project/T2I_lora_moe
开始训练
# 运行训练脚本
python train.py --learning_rate=1e-4 --batch_size=4 --epochs=10
# 或者使用你的具体参数
python train.py --args...
4. 分离 tmux 会话
当需要断开 SSH 连接时,不要直接关闭终端,而是分离 tmux 会话:
方法1:快捷键
-
按下
Ctrl+B(先按 Ctrl+B 然后释放) -
再按
D键
方法2:命令行
tmux detach
分离后你会看到类似消息:[detached (from session lora_training)]
5. 安全断开 SSH
现在可以安全地关闭终端或执行:
exit
6. 重新连接后恢复会话
重新 SSH 连接到服务器后,恢复之前的会话:
# 查看所有 tmux 会话
tmux ls
# 恢复指定会话
tmux attach -t lora_training
参数说明:
-
attach: 附加到现有会话 -
-t lora_training: 指定目标会话名称
tmux 常用命令
会话管理
# 查看所有会话
tmux list-sessions
tmux ls
# 创建新会话
tmux new -s session_name
# 重命名当前会话
tmux rename-session new_name
# 结束会话
tmux kill-session -t session_name
# 切换会话
tmux switch -t session_name
窗口管理(在 tmux 会话内)
| 快捷键 | 功能 |
|---|---|
| Ctrl+B, C | 创建新窗口 |
| Ctrl+B, 数字 | 切换到指定窗口 |
| Ctrl+B, N | 切换到下一个窗口 |
| Ctrl+B, P | 切换到上一个窗口 |
| Ctrl+B, & | 关闭当前窗口 |
| Ctrl+B, , | 重命名当前窗口 |
窗格管理(分割屏幕)
| 快捷键 | 功能 |
|---|---|
| Ctrl+B, % | 垂直分割窗格 |
| Ctrl+B, " | 水平分割窗格 |
| Ctrl+B, 方向键 | 切换窗格 |
| Ctrl+B, O | 顺时针切换窗格 |
| Ctrl+B, X | 关闭当前窗格 |
| Ctrl+B, Z | 最大化/恢复当前窗格 |
实用技巧
1. 启动时自动运行命令
tmux new -s training -d 'conda activate your_env && cd /path/to/project && python train.py'
2. 在脚本中自动化
创建脚本 start_training.sh:
#!/bin/bash
SESSION_NAME="lora_training"
# 如果会话不存在则创建
tmux has-session -t $SESSION_NAME 2>/dev/null
if [ $? != 0 ]; then
tmux new-session -d -s $SESSION_NAME
tmux send-keys -t $SESSION_NAME "conda activate your_env" C-m
tmux send-keys -t $SESSION_NAME "cd /new_disk/lmx/.project/T2I_lora_moe" C-m
tmux send-keys -t $SESSION_NAME "python train.py --args..." C-m
fi
# 附加到会话
tmux attach -t $SESSION_NAME
3. 查看训练日志
# 不附加会话的情况下查看输出
tmux capture-pane -t lora_training -p
4. 配置 tmux(~/.tmux.conf)
# 设置前缀键为 Ctrl+A(可选)
set-option -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix
# 设置状态栏
set-option -g status-interval 1
set-option -g status-justify centre
set-option -g status-bg black
set-option -g status-fg white
# 启用鼠标支持
set-option -g mouse on
故障排除
1. 找不到 tmux 会话
# 确保会话存在
tmux ls
# 如果忘记会话名,列出所有会话
tmux list-sessions
2. 会话卡住或无响应
# 强制杀死会话
tmux kill-server
# 或杀死特定会话
tmux kill-session -t session_name
3. 无法分离会话
-
尝试
Ctrl+B, D多次 -
或者尝试
Ctrl+D退出 shell
4. 多个用户冲突
如果多人使用同一服务器,可以为会话添加用户前缀:
tmux new -s username_lora_training
最佳实践
-
使用有意义的会话名:便于识别和管理
-
定期保存模型检查点:即使服务器重启也不怕
-
记录会话日志:
tmux new -s training -d "script -f training.log; python train.py" -
使用脚本自动化:减少手动操作错误
-
监控资源使用:在另一个窗口中监控 GPU 和内存
通过 tmux,你可以轻松管理长时间的深度学习训练任务,无需担心网络断开导致的中断,大大提高工作效率和稳定性。
更多推荐


所有评论(0)