Zlx:终端进程会话持久化
Zmx: Session Persistence for Terminal Processes

原始链接: https://github.com/neurosnap/zmx

## zmx:终端会话持久化 zmx 是一个轻量级工具,用于持久化终端会话,为 tmux 等功能齐全的终端复用器提供替代方案。它允许您分离并重新连接到 shell 会话,而不会丢失其状态——包括滚动和输出——*仅*关注持久性,并将窗口管理委托给您操作系统的窗口管理器。 **主要特性:** * **持久性:** 即使断开连接后也能保持 shell 会话的存活。 * **连接/分离:** 无缝重新连接到现有会话。 * **原生特性:** 利用您的终端模拟器的原生滚动和复制/粘贴功能。 * **SSH 集成:** 与 SSH 配合良好,通过 SSH 配置和 autossh 等工具实现持久的远程会话。 * **简单命令:** `zmx attach `, `zmx detach`, `zmx list`, `zmx kill`。 zmx 利用 `libghostty-vt` 来恢复终端状态,确保流畅的重新连接体验。它通过 Unix 套接字进行通信,并将活动记录到 `/tmp/zmx`。目前,配置选项有限,优先考虑可维护性。 项目和源代码地址:[https://github.com/shell-pool/shpool](https://github.com/shell-pool/shpool)

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 工作 | 提交 登录 Zmx:终端进程的会话持久化 (github.com/neurosnap) 15 分,birdculture 4小时前 | 隐藏 | 过去 | 收藏 | 3 评论 000ooo000 25分钟前 | 下一个 [–] 太棒了,我一直在想类似的东西对我在家使用会很有用。这几乎完美,并且 readme 中的 autossh + ssh '别名' (d.term) 对我来说是新的,所以学到了新知识。谢谢分享。回复 planckscnst 34分钟前 | 上一个 [–] 如果你喜欢这个工具,你可能也会对 reptyr 感兴趣,它允许你将进程重新分配到不同的 tty。https://blog.nelhage.com/2011/02/changing-ctty/ 回复 ranger_danger 25分钟前 | 父级 [–] 还有一些与 reptyr 类似的工具: https://github.com/jerome-pouiller/reredirect/ https://github.com/pasky/retty https://github.com/trishume/telefork 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

session persistence for terminal processes

Reason for this tool: You might not need tmux

  • Persist terminal shell sessions (pty processes)
  • Ability to attach and detach from a shell session without killing it
  • Native terminal scrollback
  • Multiple clients can connect to the same session
  • Re-attaching to a session restores previous terminal state and output
  • Works on mac and linux
  • This project does NOT provide windows, tabs, or splits
  • Requires zig v0.15
  • Clone the repo
  • Run build cmd
zig build -Doptimize=ReleaseSafe --prefix ~/.local
# be sure to add ~/.local/bin to your PATH

Important

Press ctrl+\ to detach from the session.

Usage: zmx <command> [args]

Commands:
  [a]ttach <name> [command...]  Create or attach to a session
  [d]etach                      Detach all clients from current session  (ctrl+\ for current client)
  [l]ist                        List active sessions
  [k]ill <name>                 Kill a session and all attached clients
  [h]elp                        Show this help message
zmx attach dev              # start a shell session
zmx attach dev nvim .       # start nvim in a persistent session
zmx attach build make -j8   # run a build, reattach to check progress
zmx attach mux dvtm         # run a multiplexer inside zmx

When you attach to a zmx session, we don't provide any indication that you are inside zmx. We do provide an environment variable ZMX_SESSION which contains the session name.

We recommend checking for that env var inside your prompt and displaying some indication there.

functions -c fish_prompt _original_fish_prompt 2>/dev/null

function fish_prompt --description 'Write out the prompt'
  if set -q ZMX_SESSION
    echo -n "[$ZMX_SESSION] "
  end
  _original_fish_prompt
end

todo.

todo.

The entire argument for zmx instead of something like tmux that has windows, panes, splits, etc. is that job should be handled by your os window manager. By using something like tmux you now have redundent functionality in your dev stack: a window manager for your os and a window manager for your terminal. Further, in order to use modern terminal features, your terminal emulator and tmux need to have support for them. This holds back the terminal enthusiast community and feature development.

Instead, this tool specifically focuses on session persistence and defers window management to your os wm.

Using zmx with ssh is a first-class citizen. Instead of sshing into your remote system with a single terminal and n tmux panes, you open n terminals and run ssh for all of them. This might sound tedious, but there are tools to make this a delightful workflow.

First, create an ssh config entry for your remote dev server:

Host = d.*
    HostName 192.168.1.xxx

    RemoteCommand zmx attach %k
    RequestTTY yes
    ControlPath ~/.ssh/cm-%r@%h:%p
    ControlMaster auto
    ControlPersist 10m

Now you can spawn as many terminal sessions as you'd like:

ssh d.term
ssh d.irc
ssh d.pico
ssh d.dotfiles

This will create or attach to each session and since we are using ControlMaster the same ssh connection is reused for every call to ssh for near-instant connection times.

Now you can use the autossh tool to make your ssh connections auto-reconnect. For example, if you have a laptop and close/open your laptop lid it will automatically reconnect all your ssh connections:

Or create an alias/abbr:

abbr -a ash "autossh -M 0 -q"
ash d.term
ash d.irc
ash d.pico
ash d.dotifles

Wow! Now you can setup all your os tiling windows how you like them for your project and have as many windows as you'd like, almost replicating exactly what tmux does but with native windows, tabs, splits, and scrollback! It also has the added benefit of supporting all the terminal features your emulator supports, no longer restricted by what tmux supports.

Each session gets its own unix socket file. Right now, the default location is /tmp/zmx. At the moment this is not configurable.

We store global logs for cli commands in /tmp/zmx/logs/zmx.log. We store session-specific logs in /tmp/zmx/logs/{session_name}.log. These logs rotate to .old after 5MB. At the moment this is not configurable.

At this point, nothing is configurable. We are evaluating what should be configurable and what should not. Every configuration option is a burden for us maintainers. For example, being able to change the default detach shortcut is difficult in a terminal environment.

  • Write programs that solve a well defined problem.
  • Write programs that behave the way most users expect them to behave.
  • Write programs that a single person can maintain.
  • Write programs that compose with other smol tools.
  • Write programs that can be finished.
  • bug: unix socket files not always getting removed properly
  • bug: remove log files when closing session
  • bug: send resize event when a client first sends stdin
  • feat: binary distribution (e.g. aur, ppa, apk, brew)
  • The daemon and client processes communicate via a unix socket
  • Both daemon and client loops leverage poll()
  • Each session creates its own unix socket file /tmp/zmx/*
  • We restore terminal state and output using libghostty-vt

We use libghostty-vt to restore the previous state of the terminal when a client re-attaches to a session.

How it works:

  • user creates session zmx attach term
  • user interacts with terminal stdin
  • stdin gets sent to pty via daemon
  • daemon sends pty output to client and ghostty-vt
  • ghostty-vt holds terminal state and scrollback
  • user disconnects
  • user re-attaches to session
  • ghostty-vt sends terminal snapshot to client stdout

In this way, ghostty-vt doesn't sit in the middle of an active terminal session, it simply receives all the same data the client receives so it can re-hydrate clients that connect to the session. This enables users to pick up where they left off as if they didn't disconnect from the terminal session at all. It also has the added benefit of being very fast, the only thing sitting in-between you and your PTY is a unix socket.

Below is a list of projects that inspired me to build this project.

You can find the source code at this repo: https://github.com/shell-pool/shpool

shpool is a service that enables session persistence by allowing the creation of named shell sessions owned by shpool so that the session is not lost if the connection drops.

shpool can be thought of as a lighter weight alternative to tmux or GNU screen. While tmux and screen take over the whole terminal and provide window splitting and tiling features, shpool only provides persistent sessions.

The biggest advantage of this approach is that shpool does not break native scrollback or copy-paste.

You can find the source code at this repo: https://github.com/martanne/abduco

abduco provides session management i.e. it allows programs to be run independently from its controlling terminal. That is programs can be detached - run in the background - and then later reattached. Together with dvtm it provides a simpler and cleaner alternative to tmux or screen.

联系我们 contact @ memedata.com