Skip to content

tmux:断线续跑与分屏 ​

SSH 断开时,前台进程会收到 SIGHUP 被杀。tmux 让会话脱离终端独立存在,重新连上就能接着看。

一、最小用法 ​

bash
tmux new -s deploy

进入后看起来和普通终端一样,但它已经在一个会话里。

bash
# 在会话里按 Ctrl+b,松开,再按 d
[detached (from session deploy)]
bash
tmux ls
text
deploy: 1 windows (created Tue Oct 27 23:55:02 2026)
bash
tmux attach -t deploy

前缀键

tmux 的所有快捷键都先按 Ctrl+b(松开)再按功能键。本文写作 Prefix + 键。

如果 Ctrl+b 和 shell 的"上一字符"冲突,可在 .tmux.conf 里改成 Ctrl+a。

二、会话、窗口、面板 ​

三层结构:session → window → pane。

会话 ​

操作命令 / 快捷键
新建tmux new -s name
列出tmux ls
进入tmux attach -t name
脱离Prefix + d
重命名tmux rename-session -t old new
杀掉tmux kill-session -t name
杀全部tmux kill-server

窗口 ​

操作快捷键
新建Prefix + c
下一个 / 上一个Prefix + n / p
按编号切换Prefix + 0..9
列出Prefix + w
重命名Prefix + ,
关闭Prefix + &

面板 ​

操作快捷键
左右分Prefix + %
上下分Prefix + "
切换Prefix + 方向键
关闭Prefix + x
最大化/还原Prefix + z
调整大小Prefix + Ctrl+方向键
text
┌────────────────────┬────────────────────┐
│  tail -F app.log   │  top -Hp 28441     │
│                    ├────────────────────┤
│                    │  ss -lntp          │
└────────────────────┴────────────────────┘

三、滚动查看历史 ​

tmux 的滚轮默认不生效(或在某些终端里失效),要用复制模式:

text
Prefix + [        进入复制模式
↑ / ↓ 或 PageUp   滚动
/                 向上搜索
?                 向下搜索
q                 退出

鼠标模式(.tmux.conf):

ini
set -g mouse on

开启后可以直接滚轮滚动、拖动选中。注意:开启鼠标模式后,终端自身的选中复制会被 tmux 接管(按住 Shift 可以绕过)。

四、一个实用的 .tmux.conf ​

ini
# ~/.tmux.conf
set -g mouse on
set -g history-limit 50000
set -g base-index 1
setw -g pane-base-index 1
set -g renumber-windows on

# 更快的前缀(可选)
# set -g prefix C-a
# unbind C-b
# bind C-a send-prefix

# 分屏用更顺手的键
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

# 面板间用 vim 风格移动
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# 状态栏
set -g status-interval 5
set -g status-left "[#S] "
set -g status-right "%Y-%m-%d %H:%M"
bash
tmux source-file ~/.tmux.conf

-c "#{pane_current_path}" 让新面板继承当前目录,不用每次 cd。

五、典型场景 ​

跑长任务 ​

bash
tmux new -s backup
# 在里面
/opt/app/bin/backup.sh
# 断线/关窗口都没关系

回来后:

bash
tmux attach -t backup

边看日志边操作 ​

bash
tmux new -s ops
# 左边
tail -F /var/log/nginx/error.log
# Prefix + % 开右边
watch -n2 'ss -s'

同时操作多台机器 ​

bash
tmux new -s multi
# 四个面板分别 ssh 到四台
ssh kafka-1
# Prefix + o 切换,或用 Prefix + z 最大化某个面板

六、和 nohup / screen 的对比 ​

方式断线续跑回看输出交互适用
nohup cmd &是只能看日志文件否一次性脚本
screen是是是老系统,tmux 不在
tmux是是是首选
systemd是journald否长期服务

nohup 不等于绝对安全

nohup cmd & 只是屏蔽 SIGHUP 并重定向输出。如果机器重启、OOM、或者有人 kill,进程照样没了。

长期服务应该用 systemd;临时长任务用 tmux;nohup 只适合"跑完就退出"的脚本。

七、排障 ​

bash
tmux ls
text
error connecting to /tmp/tmux-0/default (No such file or directory)

说明没有运行中的 server,直接 tmux new 即可。

现象原因处置
attach 后画面乱码终端类型不匹配tmux -u,或设 TERM=xterm-256color
滚轮不能滚没开 mouseset -g mouse on
会话在别的用户下tmux 按用户隔离sudo -u app tmux ls
lost serverserver 被 kill / 机器重启会话已丢失,无法恢复
复制粘贴不好用tmux 接管了鼠标按住 Shift 选中,或进复制模式
中文显示为方块终端字体/编码终端设 UTF-8,tmux -u

八、踩过的坑 ​

现象根因处置
断线后任务没了没用 tmux/nohup长任务先进 tmux
tmux attach 报 no sessions机器重启过会话不持久,只能重跑
分屏后目录不同默认继承启动目录配置 -c "#{pane_current_path}"
关掉终端窗口会话也没了关的是"终端模拟器"且 tmux 未 detach先 Prefix+d,再关窗口
Prefix 没反应输入法/终端截获了 Ctrl+b换前缀键
历史行数太少默认 2000 行history-limit 50000