# LibTmux.Server.with_owned/2

- **Module:** LibTmux
- **Package:** libtmux
- **Language:** Elixir
- **Kind:** function
- **Source:** https://github.com/libtmux/libtmux-elixir/blob/00d34b3d387da72a5a416f1dd3962a98c76cedd2/lib/libtmux/server.ex#L381
- **Page:** https://libtmux.org/en/elixir/latest/reference/libtmux-server-with_owned-2/

```
with_owned(options, fun)
@spec with_owned([start_option()], (t() -> result)) ::
        result
      when result: term()
```

Starts a private server, calls `fun` with it and stops it afterwards.

Takes the options of `start/1`. Returns the value of `fun`. A start failure
raises its `LibTmux.Error` before `fun` runs. The server stops when `fun`
returns, raises, throws or exits. If `fun` completes and the stop fails,
`with_owned/2` raises the stop error; a failing stop never replaces an
exception from `fun`.

    alias LibTmux.Server

    Server.with_owned([executable: System.find_executable("tmux")], fn server ->
      {:ok, _} = Server.new_session(server, "demo", command: {:argv, ["/bin/cat"]})
      Server.snapshot!(server)
    end)

## Type parameters

- `result` (bound `term()`)

## Parameters

- `options` (`[start_option()]`)
- `fun` (`(t() -> result)`)

## Guides

Pages that discuss `with_owned/2`.

- [Capture a pane and read it](/en/elixir/latest/examples/capture-and-read/)
