# libtmux.OwnedServer:stop

- **Module:** libtmux
- **Package:** libtmux
- **Language:** Lua
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-lua/blob/ec2c4d710bcec2bd54c1ded405e6652e873b0994/lua/libtmux/_internal/server.lua#L1235
- **Page:** https://libtmux.org/en/lua/latest/reference/libtmux-ownedserver-stop/

```
stop: fun(
  self: libtmux.OwnedServer,
):libtmux.Request<boolean>
```

Runs [`kill-server`](/en/tmux/latest/manual/kill-server/), then connects to the socket until nothing listens, for
 at most `timeout` milliseconds, and removes the directory. It resolves to
 `true` once the daemon is gone and the directory removed, and again on later
 calls. A daemon that still answers fails with `stop_failed`, keeps the
 directory and names it in `directory`; calling again tries once more. When
 the program left another tmux server under the directory, `stop` stops it
 too and fails with `leaked_server`, naming each socket in `sockets`.

## Examples

Start a private tmux server, create a session on it, and stop it. Use Lua 5.1 or newer with LuaRocks, tmux 3.3a or newer, and a C compiler and CMake for luv on Linux. In a new directory, install the library and its standalone runtime dependency from LuaRocks:

```console
$ luarocks --tree ./rocks install luv &&
  luarocks --tree ./rocks install libtmux &&
  eval "$(luarocks --tree ./rocks path)"
```

Save the complete program as `start_server.lua`. It starts its own private tmux server, uses it and stops it, so no existing session is needed.

[Source example](https://github.com/libtmux/libtmux-lua/blob/ec2c4d710bcec2bd54c1ded405e6652e873b0994/examples/api/start_server.lua).

```lua
local adapter = require("libtmux.runtime.luv")

local function must(value, err)
    if err ~= nil then
        error(tostring(err), 0)
    end
    return value
end

-- tmux is /usr/bin/tmux unless TMUX_BIN names another absolute path.
local binary = os.getenv("TMUX_BIN") or "/usr/bin/tmux"

must(adapter.run(function(runtime)
    -- The server is private: its own directory, socket and environment.
    local owned = must(runtime:start_server({ binary = binary }):await())
    local server = owned.server
    local before = must(server:snapshot():await())

    must(server:new_session({ name = "demo", argv = { "/bin/cat" } }):await())
    local after = must(server:snapshot():await())
    print(("sessions: %d, then %d"):format(#before.sessions, #after.sessions))

    must(owned:stop():await())
    return true
end))
```

Run the saved program. It runs `/usr/bin/tmux`; set `TMUX_BIN` to another absolute path to use a different binary. Assertions and operation errors produce a failing exit status:

```console
$ eval "$(luarocks --tree ./rocks path)" &&
  lua start_server.lua
```

## Guides

Pages that discuss `stop`.

- [Getting started](/en/lua/latest/guides/overview/)
- [Runtime ownership](/en/lua/latest/guides/runtime/)
