Yes, in Elixir, you can achieve all of the functionality you're looking for using the built-in Erlang module :erlang.open_port/2 or more conveniently using the excellent Hex package called Porcelain or MuonTrap.
MuonTrap is a modern and robust library for running and managing external OS processes in Elixir. It is actively maintained and used in production systems (e.g., Nerves).
Features:
Start executables with custom environment variables and arguments
Read STDOUT and STDERR in real-time
Gracefully stop processes (sends SIGINT or SIGTERM)
Handles zombie processes and signals properly (especially useful on Linux)
Example:
elixir
# Add to mix.exs
def deps do
[
{:muontrap, "~> 0.10"}
]
end
elixir
# Start a process
{:ok, pid} = MuonTrap.Daemon.start_link(
"my_executable",
["--arg1", "value"],
env: %{"PORT" => "1138"},
stderr_to_stdout: true
)
# Read output
receive do
{:stdout, ^pid, data} ->
IO.puts("Got output: #{data}")
end
# Stop the process
MuonTrap.Daemon.stop(pid)
Porcelain was a popular choice for process management in Elixir, but it is no longer actively maintained and has some issues with zombie processes on Linux.
Example:
elixir
# Add to mix.exs
def deps do
[
{:porcelain, "~> 2.0"}
]
end
elixir
# Start a process
opts = [env: %{"PORT" => "1138"}, out: :stream, err: :stream]
proc = Porcelain.spawn("my_executable", ["--arg1", "value"], opts)
# Read output
receive do
{_, :data, output} ->
IO.puts("Output: #{output}")
end
# Stop the process
Porcelain.Process.stop(proc)
Built-in Option: :erlang.open_port/2
You can also use Erlang's built-in :open_port/2, but it's lower-level and more complex to use correctly. Libraries like MuonTrap wrap this functionality in a safer and more ergonomic way.
Summary
Feature
MuonTrap ✅
Porcelain ⚠️
:open_port/2 ⚙️
Start with ENV + args
✅
✅
✅
Stop process gracefully
✅
⚠️
⚠️
Read STDOUT/STDERR
✅
✅
✅
Actively maintained
✅
❌
✅
Handles signals/zombies
✅
❌
⚠️
✅ Recommendation:
Use MuonTrap for robust and production-safe process management in Elixir.