Server
LibTmux Connects to an existing endpoint. Closing retires clients, never the daemon.
Source and package
- Module
- LibTmux
- Package
- libtmux
- Source
- gems/libtmux/lib/libtmux/terminal.rb
- Other languages
- PowerShell (cmdlet)Julia (struct)PythonLuaTypeScriptRustGoJavaC#C++SwiftKotlinScalaF# (module)
-
Description#
A server is bound to one tmux executable and one Unix socket. It keeps a private link to the socket it bound, so commands never reach a different daemon that later takes over the same path. An
EntityRefis valid only on the server that issued it; another server raisesTargetNotFoundError.Methods that run a tmux command take
timeout:andcancel:. The typed methods raiseCommandErrorwhen tmux rejects a command, and a listing that fails raises, so an empty array means tmux answered with nothing to list.#runand#run_groupanswer their result whatever tmux's exit status.max_requests:bounds concurrent commands andmax_controls:bounds control connections; a call past either limit raisesCapacityErrorinstead of waiting. After#closethe server refuses new requests withClosedError, and#diagnosticsstill answers. A forked child can close an inherited server but cannot use it.What is on here
The methods fall into the groups below. A
Session,Window, orPanecarries the operations on that one thing.Connecting.
Server.newandServer.openbind an endpoint that already exists;Server.openwith a block closes the server when the block ends.Server.startstarts a private daemon instead, creates no session, and owns the daemon's exit.#endpointanswers theEndpointand#owned?says whether this server started its daemon.#diagnosticsreports the request and control slots in use, and the limits.#closeretires the server's clients; on an owned server it also stops the daemon.#open_controlopens aControlConnectionto a session.Finding one thing.
#session,#window,#pane, and#window_linkwrap anEntityRefin a handle without running a command. Take the ref from a handle or a snapshot record. A ref of the wrong kind raisesTargetNotFoundError; one whose entity has gone fails at its first command.Listing everything.
#list_sessions,#list_windows, and#list_panesanswer frozen arrays of handles in numeric ID order, and#list_window_linksanswers everyWindowLink.#list_clientsanswers one hash per client.#snapshotcaptures the session, window, and pane tree, and clients withclients: true, as one validatedSnapshot.Querying things and getting around.
#search_panesfilters the panes of a snapshot withwhere:, a hash of criteria or aFilterExpr, and answers aSelection;#explain_panesanswers the plan for a query without running tmux. ASelectionnarrows withSelection#where,Selection#select,Selection#filter, andSelection#reject, tests withSelection#exists?, and takes a single match withSelection#one, which raisesNoMatchErrororMultipleMatchesErrorotherwise, orSelection#one_or_nil. Build and compose criteria withFilterExpr.build,FilterExpr#and,FilterExpr#or,FilterExpr#not, andFilterExpr.from_json.A
Snapshotanswers relational questions without another command:Snapshot#sessions,Snapshot#windows,Snapshot#panes, andSnapshot#clients. Its records follow relations by name: aSessionSnapshotreacheswindows,panes, andcurrent_window, aWindowSnapshotreachespanesandactive_pane, and aPaneSnapshotreaches itswindow.Entity#snapshotreads one handle back as a record, andCapturedRecord#refwith#session,#window, or#paneturns a record into a handle. Live handles go down one command at a time withSession#list_windows,Session#list_panes, andWindow#list_panes, and back up withEntity#server.Changing things.
#new_sessionstarts a detached session runningcommand:, an argv array rather than a shell string, and answers theSessionor, withreceipt: true, aCreationReceipt.#killstops the tmux server, which#closedoes only for an owned server.#switch_clientpoints the client named byclient:at a session.Options and hooks.
#optionsanswersOptionsat server scope, or at the global session or window scope.#hooksanswersHooksat the global session or window scope.#environment,#set_environment, and#unset_environmentread, set, and unset one variable in the server's global environment.Everything else tmux keeps. Paste buffers:
#list_buffers,#read_buffer,#write_buffer, and#delete_buffer.#source_fileloads a configuration file.#displayexpands a tmux format; the argument is a format, not literal text.#wait_forwaits on, signals, locks, or unlocks a channel, asaction:says.Things that need a terminal.
#attachruns an interactive client on a TTY the caller supplies and keeps, blocks until that client exits, and answers aTerminalResult. Cancelling detaches the client and does not stop pane commands.Anything else tmux can do is reachable through
#run, which runs one command from an argv array and keeps tmux's separators and format semantics, or#run_group, which runs several in one tmux invocation. AGroupResultcannot attribute the client's final status to a single step.Examples#
Connect a Server client to an existing endpoint and close it without stopping the daemon. The outer Server.start block owns the private demonstration server. Use Ruby 4.0.7, Bundler, Git and tmux 3.2a or newer on Unix, with /bin/cat and /bin/sh available. In an empty directory, fetch the documented library revision:
Terminal window $ git clone https://github.com/libtmux/libtmux-ruby.git libtmux-source && \git -C libtmux-source checkout dcc9f8b5a6970144a9e7d80cd5f381d8f6198372Save this dependency file as Gemfile:
source "https://rubygems.org"gem "libtmux", path: "libtmux-source/gems/libtmux"Save this complete program as server.rb. Its Server.start block owns a private tmux server and stops it on exit, including after an error. View source
# frozen_string_literal: truerequire "libtmux"beginLibTmux::Server.start do |owner|owner.new_session(name: "work", command: ["/bin/cat"])server = LibTmux::Server.new(endpoint: owner.endpoint)beginputs "connected sessions: #{server.list_sessions.length}"ensureserver.closeendputs "sessions after client close: #{owner.list_sessions.length}"endrescue StandardError => errorwarn "#{error.class}: #{error.message}"exit 1endInstall the runtime dependencies and run the saved program. An operation or cleanup error makes it exit unsuccessfully:
Terminal window $ bundle config set --local path vendor/bundle && \bundle install --jobs 2 && \bundle exec ruby server.rbExpected output:
connected sessions: 1sessions after client close: 1In other ports A tmux server
- PowerShell
LibTmux\New-TmuxServerThis cmdlet constructs a shared .NET
LibTmux.Serverhandle without contacting tmux. - Julia
LibTmux.ServerServerdescribes an endpoint without contacting tmux. Read its hierarchy withsnapshot. - Python
- Lua
- TypeScript
- Rust
- Go
- Java
- C#
- C++
- Swift
- Kotlin
- Scala
- F#
LibTmux.FSharp.ServerThis F# module provides functions over the shared .NET
LibTmux.Servertype.
- PowerShell
Members39#
Collections and queries7#
- list_sessions method Behavior: Live acquisition.
- list_windows method Behavior: Live acquisition.
- list_panes method Behavior: Live acquisition.
- list_clients method Behavior: Live acquisition.
- list_buffers method Behavior: Buffers.
- snapshot method Behavior: Snapshot acquisition.
- search_panes method Behavior: Source query execution.
Common operations19#
- new_session method Behavior: Creation.
- kill method Behavior: Killing daemons.
- source_file method Behavior: Source files.
- options method Behavior: Options and hooks.
- hooks method Behavior: Options and hooks.
- write_buffer method Behavior: Buffers.
- environment method Behavior: Environment.
- read_buffer method Behavior: Buffers.
- delete_buffer method Behavior: Buffers.
- run method Raw tmux arguments retain tmux's separators and format semantics.
- set_environment method Behavior: Environment.
- unset_environment method Behavior: Environment.
- wait_for method Retiring the client cannot remove a dispatched waiter from tmux's queue.
- session method Behavior: Live handles.
- window method Behavior: Live handles.
- pane method Behavior: Live handles.
- open method Behavior: Borrowed bindings.
- start method Starts a foreground daemon on a new private endpoint. Close owns its exit. No session is created; the default config is empty. Startup blocks.
- new method Behavior: Borrowed bindings.
Other members13#
- attach method Blocks until the owned interactive client exits. The caller supplies and retains its TTY. Cancellation detaches this client, not pane commands.
- close method Behavior: Closing bindings.
- diagnostics method Returns a frozen local snapshot, including after close. Slots are admitted requests; control connections are retained registrations, not OS clients.
- display method Formats are intentionally executable tmux formats, not literal text.
- endpoint method Returns the value of attribute endpoint.
- explain_panes method Behavior: Source query plans.
- list_window_links method Behavior: Live acquisition.
- open_control method Behavior: Control connections.
- owned? method Behavior: Borrowed bindings.
- run_group method One nontransactional tmux command group, with literal arguments per command.
- switch_client method Resolves the literal selector at dispatch; it is not a client reference.
- tmux_version method The daemon's release text, such as "3.7d" or "next-3.9".
- window_link method Behavior: Live handles.