Raxol.System.PortCommand (Raxol v2.7.0)

View Source

Utility for running external commands with stdin input via Port.

This module provides a way to execute external commands and pass data to their stdin, which System.cmd/3 doesn't support directly.

Why the stdin goes through a temp file

A :spawn_executable port cannot half-close: the only way to signal EOF on the child's stdin is Port.close/1, which also tears the port down before its {:exit_status, _} is delivered. Commands that read stdin to EOF (pbcopy, xclip, flamegraph.pl) therefore never finished under a plain command/close sequence, and the caller blocked the full timeout. Feeding stdin from a temporary file via a shell redirect gives the child a real EOF, so it exits on its own and the port reports its status like any self-terminating process.

Summary

Functions

Runs a command with the given arguments, passing input to stdin.

Functions

run(command, args, input, opts \\ [])

@spec run(String.t(), [String.t()], String.t(), keyword()) ::
  {:ok, String.t()} | {:error, String.t()}

Runs a command with the given arguments, passing input to stdin.

Returns {:ok, output} on success (exit code 0) or {:error, output} on failure.

Options

  • :timeout - Maximum time to wait for the command in milliseconds (default: 30000)

Examples

iex> PortCommand.run("cat", [], "hello")
{:ok, "hello"}

iex> PortCommand.run("grep", ["pattern"], "no match here")
{:error, ""}