关于 tmux
一项使用 tmux 运行和管理后台进程的技能,涵盖会话、窗口与窗格操作、输出捕获,以及供 Agent 使用的隔离套接字。
主要功能
- 01创建和管理会话
- 02控制窗口与窗格
- 03发送命令并捕获输出
- 04为 Agent 使用专用套接字
适用场景
- 01在后台运行开发服务器
- 02将测试输出捕获到文件
- 03在不同窗口管理多个服务
- 04监控长时间运行的构建
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ johnlindquist/ claude --skill tmux安装后在 Claude Code 中输入/tmux确认已加载。
详细介绍
Tmux Session Manager
使用 tmux 运行和管理后台进程。
前置条件
安装 tmux:
brew install tmux
# or
apt install tmux
命令行参考
创建会话
# Basic session
tmux new-session -d -s mysession
# With window name
tmux new-session -d -s mysession -n mywindow
# With working directory
tmux new-session -d -s mysession -c /path/to/dir
# With initial command
tmux new-session -d -s mysession "npm run dev"
# Combined
tmux new-session -d -s dev -n server -c /project "npm run dev"
列出会话
# List all sessions
tmux list-sessions
# Short format
tmux ls
# With format
tmux list-sessions -F "#{session_name}: #{session_windows} windows"
向会话发送按键
# Send command
tmux send-keys -t mysession "npm test" Enter
# Send to specific window
tmux send-keys -t mysession:mywindow "command" Enter
# Send without Enter
tmux send-keys -t mysession "partial command"
# Send special keys
tmux send-keys -t mysession C-c # Ctrl+C
tmux send-keys -t mysession C-d # Ctrl+D
tmux send-keys -t mysession Escape # Escape
tmux send-keys -t mysession Up # Arrow up
捕获输出
# Capture visible pane
tmux capture-pane -t mysession -p
# Capture with history (last N lines)
tmux capture-pane -t mysession -p -S -100
# Capture all history
tmux capture-pane -t mysession -p -S -
# Save to file
tmux capture-pane -t mysession -p > output.txt
终止会话
# Kill specific session
tmux kill-session -t mysession
# Kill all sessions
tmux kill-server
附加到会话
# Attach
tmux attach -t mysession
# Attach or create
tmux new-session -A -s mysession
窗口管理
# New window
tmux new-window -t mysession -n windowname
# Select window
tmux select-window -t mysession:windowname
# Kill window
tmux kill-window -t mysession:windowname
# List windows
tmux list-windows -t mysession
窗格管理
# Split horizontally
tmux split-window -t mysession -h
# Split vertically
tmux split-window -t mysession -v
# Select pane
tmux select-pane -t mysession:0.1
# Kill pane
tmux kill-pane -t mysession:0.1
工作流模式
在后台运行开发服务器
# Start server
tmux new-session -d -s devserver -c /project "npm run dev"
# Check output
tmux capture-pane -t devserver -p -S -50
# Stop server
tmux send-keys -t devserver C-c
tmux kill-session -t devserver
运行测试并捕获输出
# Start tests
tmux new-session -d -s tests "npm test"
# Wait and capture output
sleep 30
tmux capture-pane -t tests -p -S - > test-output.txt
# Cleanup
tmux kill-session -t tests
多个服务
# Create session with multiple windows
tmux new-session -d -s services -n frontend
tmux new-window -t services -n backend
tmux new-window -t services -n database
# Start each service
tmux send-keys -t services:frontend "npm run dev" Enter
tmux send-keys -t services:backend "go run main.go" Enter
tmux send-keys -t services:database "docker-compose up db" Enter
# Check all services
for win in frontend backend database; do
echo "=== $win ==="
tmux capture-pane -t services:$win -p -S -10
done
长时间运行的进程
# Start process
tmux new-session -d -s build "npm run build:production"
# Monitor progress
watch -n 5 "tmux capture-pane -t build -p -S -20"
# Or periodic check
while tmux has-session -t build 2>/dev/null; do
sleep 10
echo "Still running..."
done
echo "Build complete"
Socket 模式(适用于 Agent)
在 Agent 上下文中运行时,使用专用 socket:
SOCKET_PATH="/tmp/agent-tmux-$$"
# Create session with socket
tmux -S $SOCKET_PATH new-session -d -s agent "command"
# Send keys via socket
tmux -S $SOCKET_PATH send-keys -t agent "input" Enter
# Capture via socket
tmux -S $SOCKET_PATH capture-pane -t agent -p
# Cleanup
tmux -S $SOCKET_PATH kill-server
rm -f $SOCKET_PATH
最佳实践
- 为会话起描述性名称 - 便于之后识别
- 使用
-d以分离模式运行 - 不阻塞终端 - 定期捕获输出 - 在终止会话之前
- 设置工作目录 - 使用
-c /path提供上下文 - 清理会话 - 完成后终止
- 使用 socket 实现隔离 - 防止 Agent 之间的冲突
评论
支持 Markdown