AgentSkillsCN

tmux

介绍如何使用 tmux 创建多个进程、实时监控进程,并捕获其输出。适用于运行服务器、长时间运行的任务,或后台进程时使用。

SKILL.md
--- frontmatter
name: tmux
description: Instructions for using tmux to spawn multiple processes, inspect them, and capture their output. Use when running servers, long-running tasks, or background processes.

tmux

Manage background processes using tmux windows without blocking the terminal.

1. Verify Environment & Check Status

Confirm tmux is running and list existing windows:

bash
tmux list-windows

2. Spawn a Background Process

Create a new tmux window and run a command:

bash
tmux new-window -n "my-server" -d
tmux send-keys -t "my-server" "npm run dev" C-m
  • -n "my-server" names the window for easy reference
  • -d keeps it detached (runs in background)
  • C-m sends Enter to execute the command

3. Inspect Output (Read Logs)

Visible screen only

bash
tmux capture-pane -p -t "my-server"

Full scrollback history

bash
tmux capture-pane -p -t "my-server" -S -1000
  • -S -1000 captures the last 1000 lines of scrollback

4. Interact with the Process

Send Ctrl+C (interrupt)

bash
tmux send-keys -t "my-server" C-c

Kill the window

bash
tmux kill-window -t "my-server"

5. Advanced: Chaining Commands

Run multiple commands in sequence:

bash
tmux new-window -n "build" -d && \
tmux send-keys -t "build" "npm run build && npm run test" C-m

Quick Reference

ActionCommand
Create windowtmux new-window -n "ID" -d
Run commandtmux send-keys -t "ID" "CMD" C-m
Read outputtmux capture-pane -p -t "ID"
Send interrupttmux send-keys -t "ID" C-c
Kill windowtmux kill-window -t "ID"