# `MuonTrap.Daemon`
[🔗](https://github.com/fhunleth/muontrap/blob/v1.8.0/lib/muontrap/daemon.ex#L12)

Wrap an OS process in a GenServer so that it can be supervised.

For example, in your children list add MuonTrap.Daemon like this:

```elixir
children = [
  {MuonTrap.Daemon, ["my_server", ["--options", "foo"], [cd: "/some_directory"]]}
]

opts = [strategy: :one_for_one, name: MyApplication.Supervisor]
Supervisor.start_link(children, opts)
```

In the `child_spec` tuple, the second element is a list that corresponds to
the `MuonTrap.cmd/3` parameters. I.e., The first item in the list is the
program to run, the second is a list of commandline arguments, and the third
is a list of options. The same options as `MuonTrap.cmd/3` are available with
the following additions:

* `:name` - Name the Daemon GenServer
* `:logger_fun` - Pass a 1-arity function or `t:mfargs/0` tuple to replace
  the default logging behavior. When set, `:log_output`, `:log_prefix`,
  `:log_transform`,
  and `:logger_metadata` will be ignored.
* `:log_output` - When set, send output from the command to the Logger.
  Specify the log level (e.g., `:debug`)
* `:log_prefix` - Prefix each log message with this string (defaults to the
  program's path)
* `:log_transform` - Pass a function that takes a string and returns a string
  to format output from the command. Defaults to `String.replace_invalid/1`
  on Elixir 1.16+ to avoid crashing the logger on non-UTF8 output.
* `:logger_metadata` - A keyword list to merge into the process's logger metadata.
  The `:muontrap_cmd` and `:muontrap_args` keys are automatically added and
  cannot be overridden.
* `:stderr_to_stdout` - When set to `true`, redirect stderr to stdout.
  Defaults to `false`.
* `:capture_stderr_only` - When set to `true`, capture only stderr and ignore stdout.
  This is useful when you want to capture error messages but not regular output.
  Defaults to `false`.
* `:exit_status_to_reason` - Optional function to convert the exit status (a
  number) to stop reason for the Daemon GenServer. Use if error exit codes
  carry information or aren't errors.
* `:wait_for` - A 0-arity function that runs before the OS process is
  launched. Use to wait for a required resource to be available. The return
  value is ignored. Raise to abort the launch.

If you want to run multiple `MuonTrap.Daemon`s under one supervisor, they'll
all need unique IDs. Use `Supervisor.child_spec/2` like this:

```elixir
Supervisor.child_spec({MuonTrap.Daemon, ["my_server", []]}, id: :server1)
```

# `cgget`

```elixir
@spec cgget(GenServer.server(), binary(), binary()) ::
  {:ok, String.t()} | {:error, File.posix()}
```

Get the value of the specified cgroup variable.

# `cgset`

```elixir
@spec cgset(GenServer.server(), binary(), binary(), binary()) ::
  :ok | {:error, File.posix()}
```

Modify a cgroup variable.

# `child_spec`

```elixir
@spec child_spec(keyword()) :: Supervisor.child_spec()
```

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `os_pid`

```elixir
@spec os_pid(GenServer.server()) :: non_neg_integer() | :error
```

Return the OS pid to the muontrap executable.

# `start_link`

```elixir
@spec start_link(binary(), [binary()], keyword()) :: GenServer.on_start()
```

Start/link a deamon GenServer for the specified command.

# `statistics`

```elixir
@spec statistics(GenServer.server()) :: %{output_byte_count: non_neg_integer()}
```

Return statistics about the daemon

Statistics:

* `:output_byte_count` - bytes output by the process being run

---

*Consult [api-reference.md](api-reference.md) for complete listing*
