Server
Manages sessions, windows, and panes on a tmux server.
Source and package
- Module
- io.github.libtmux.kotlin
- Package
- io.github.libtmux:libtmux-kotlin
- Source
- libtmux-kotlin/src/main/kotlin/io/github/libtmux/kotlin/Server.kt
- Other languages
- PythonRubyLuaTypeScriptRustGoJavaC#C++SwiftScala F#
- public class Server : Auto
Closeable -
Description#
Use
openwith aServerConfigto select a server by socket name or path. From this object, create sessions or query the server's existing sessions, windows, and panes. EachSessioncontains windows, and eachWindowcontains panes.Operations that contact tmux are
suspendfunctions dispatched throughpolicy. Callcloseafter the last operation to release resources owned by this object. The tmux server and its sessions keep running.asJavaexposes the underlyingJavaServerfor interoperability. Both objects share the same resources, so closing either closes both.Examples#
Connect to a private tmux server. Use JDK 25, Git, and tmux 3.2a through 3.7c on Linux, 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-java.git libtmux-source && \git -C libtmux-source checkout f56392b5d9bc7f1f9c1631842333fb90f3d82d40Save this consumer setup as settings.gradle.kts: View source
rootProject.name = "api-example"includeBuild("libtmux-source")Save this consumer setup as build.gradle.kts: View source
plugins {kotlin("jvm") version "2.4.10"application}repositories { mavenCentral() }dependencies {implementation("io.github.libtmux:libtmux-kotlin:0.0.1-alpha.17-SNAPSHOT")}java { toolchain { languageVersion.set(JavaLanguageVersion.of(25)) } }kotlin { jvmToolchain(25) }application { mainClass.set(providers.gradleProperty("exampleMain")) }Save this consumer setup as gradle.properties: View source
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=768m -Dfile.encoding=UTF-8kotlin.daemon.jvmargs=-Xmx2gorg.gradle.workers.max=2Save this launcher as run.sh. It creates and stops a private tmux server, including after program failure. A shutdown failure retains its socket directory and exits unsuccessfully. View source
#!/bin/shset -euif [ "$#" -eq 0 ]; thenprintf 'Usage: sh run.sh program [arguments...]\n' >&2exit 2fibinary=$(command -v "${TMUX_BIN:-tmux}")mkdir -p /tmp/libtmux-java-devdirectory=$(mktemp -d /tmp/libtmux-java-dev/api.XXXXXX)socket="$directory/tmux.sock"config="$directory/tmux.conf"alive() {"$binary" -S "$socket" display-message -p '#{pid}' >/dev/null 2>&1}cleanup() {status=$?trap - 0 HUP INT TERMif alive; thenif ! "$binary" -S "$socket" kill-server; thenprintf 'Cannot stop tmux; kept %s\n' "$directory" >&2exit 1fiattempts=0while alive && [ "$attempts" -lt 100 ]; dosleep 0.05attempts=$((attempts + 1))doneif alive; thenprintf 'tmux is still responding; kept %s\n' "$directory" >&2exit 1fifirm -rf "$directory" || exit 1exit "$status"}trap cleanup 0trap 'exit 1' HUP INT TERMunset TMUX TMUX_PANEprintf 'set-option -g default-shell /bin/sh\n' > "$config""$binary" -S "$socket" -f "$config" new-session -d -s work-one -n editor /bin/cat"$binary" -S "$socket" split-window -h -t '=work-one:editor' /bin/cat"$binary" -S "$socket" new-window -d -t '=work-one' -n logs /bin/cat"$binary" -S "$socket" new-session -d -s work-two -n editor /bin/cat"$@" "$binary" "$socket" "$config""$binary" -S "$socket" has-session -t '=work-one'"$binary" -S "$socket" has-session -t '=work-two'Save this complete program as src/main/kotlin/Connect.kt. It opens an ordinary client for the launcher's server; closing the client releases its transport. View source
package io.github.libtmux.examples.api.kotlinimport io.github.libtmux.ServerConfigimport io.github.libtmux.ServerEndpointimport io.github.libtmux.kotlin.*import java.nio.file.Pathimport kotlin.system.exitProcessimport kotlinx.coroutines.runBlocking/** Connect to a private tmux server. */fun main(args: Array<String>) {try {require(args.size == 3) { "expected: tmux-binary socket-path config-file" }val config = ServerConfig.builder().binary(args[0]).endpoint(ServerEndpoint.socketPath(Path.of(args[1]))).configFile(Path.of(args[2])).build()runBlocking {withServer(config) { server ->println("connected=${server.isAlive()}")}}} catch (error: Exception) {if (error is InterruptedException) Thread.currentThread().interrupt()System.err.println("Example failed: ${error.message}")exitProcess(1)}}Build the minimal consumer and run the saved program with the private fixture. An operation or cleanup error makes the command exit unsuccessfully:
Terminal window $ ./libtmux-source/gradlew --project-dir . --console=plain --quiet \installDist -PexampleMain=io.github.libtmux.examples.api.kotlin.ConnectKt && \sh run.sh build/install/api-example/bin/api-exampleExpected output:
connected=trueDiscussed in Server, session, window, pane · Attaching to tmux , Coroutines and flows , Getting started
In other ports A tmux server
- Python
libtmux.Server - Ruby
LibTmux::Server - Lua
libtmux.Server - TypeScript
server.Server - Rust
server.Server - Go
tmux.Server - Java
io.github.libtmux.Server - C#
LibTmux.Server - C++
libtmux::Server - Swift
Server - Scala
io.github.libtmux.scaladsl.Server - F# No source-verified F# equivalent is recorded for this operation.
- Python
Members49 members#
Collections and queries6 members#
- sessions method Every session, captured now.
- windows method Captures every winlink, preserving each session and index placement.
- panes method Captures every pane on the server.
- clients method Captures every attached client.
- buffers method The server's paste buffers, which every session shares.
- snapshot method Captures the whole hierarchy in at most four listings, retrying once if the server is replaced.
Common operations14 members#
- newSession method Creates a detached session and returns that exact session.
- killServer method Ends the tmux server and every session on it.
- hooks method The global hooks every session inherits.
- keys method The server's key bindings:
prefixwhen binding, every table when listing. - liveState method A live, continuously rebuilt view of
server, as aStateFlow. - batch method Collects several commands to run in one tmux invocation.
- chain method Starts a chain of commands where each one acts on what the last one made.
- commands method The commands this tmux knows.
- messageLog method The server's message log.
- prompt method The command prompt's history.
- session method The one session this expression matches, captured now.
- sessionOrNull method The one session this expression matches, or null when none does.
- shell method Shell commands run by tmux, and tmux commands chosen by a shell exit status.
- withLiveState method As
liveState, scoped toblock: opens the live view, runsblockagainst it, and cancels the background pump before returning — the samewithServer/withControlshape, for the one resourceliveStateitself cannot close on its own.
Other members29 members#
- admissionBound property How many tmux commands this server's transport runs at once, from
ExecutionPolicy.default. - asJava property The Java server this wraps, for an operation this class does not mirror or a Java API that takes one. The same object, not a copy: closing either closes both.
- attachedSessions method Captures every session a client is attached to.
- channel method One of this server's wait-for channels, which is where a signal is sent and waited for.
- close method Releases an owned transport. Idempotent, and never kills tmux.
- cmd method Runs one tmux command against this server.
- Companion module
- config property How this server was configured.
- control method Attaches a control client to this session's server, and only to the process this capture named.
- environment method The server's environment, which every session inherits and every new process is given.
- expand method Expands a tmux format against the server, and answers with what it came to.
- globalOptions method The global session options every session inherits unless it sets its own.
- hasSession method Whether a session with this name exists.
- identity property Which server this is. Every handle taken from it is scoped by this.
- isAlive method Whether the server is running and answering.
- killSession method Ends the session with this name, and everything in it.
- lock method Locks every client attached to this server.
- options method The server-wide options, the ones tmux keeps once per server.
- pane method The one pane this expression matches, captured now.
- paneFields method Chosen fields for every pane on the server, from one listing.
- paneOrNull method The one pane this expression matches, or null when none does.
- policy property
- requireAlive method Requires a running tmux daemon that answers the liveness probe.
- run method Runs a command that is expected to work, and raises when it did not.
- sourceFile method Runs a file of tmux commands, as a configuration file would be run.
- variables method Reads only validated tmux variable names, never caller-authored format syntax.
- version method Which tmux this server is running.
- window method The one window link this expression matches, captured now.
- windowOrNull method The one window link this expression matches, or null when none does.