shahryarjb

shahryarjb

Hi friend,
I run 3 or 4 different command with System.cmd, so users wait for these tasks are done and after getting status 0 can do another jobs, but I need a way to show them System.cmd log line by line when is running not show all the log at the end.

System.cmd(operation, [command], into: IO.stream(), stderr_to_stdout: true, env: [{"MIX_ENV", "#{Mix.env()}"}])

And if this feature is available at any time, it will be very attractive. For example, from the middle of the log, if requested, show not the whole log or sending any line to PubSub to the channel.

Thank you

Showing Posts 1 to 9

ruslandoga

ruslandoga

You might be able to do that with Port.open and then receive messages coming from it and send them to the liveview process.

port.exs

defmodule Command do
  def exec(cmd, args) do
    path = System.find_executable(cmd)
    port = Port.open({:spawn_executable, path}, [:binary, :exit_status, args: args, line: 1000])
    loop(port)
  end

  defp loop(port) do
    receive do
      {^port, {:data, data}} ->
        IO.inspect(data: data)
        loop(port)

      {^port, {:exit_status, exit_status}} ->
        IO.inspect(exit_status: exit_status)
    end
  end
end

Command.exec("cat", ["port.exs"])
> elixir port.exs
[data: {:eol, "defmodule Command do"}]
[data: {:eol, "  def exec(cmd, args) do"}]
[data: {:eol, "    path = System.find_executable(cmd)"}]
[
  data: {:eol,
   "    port = Port.open({:spawn_executable, path}, [:binary, :exit_status, args: args, line: 1000])"}
]
[data: {:eol, "    loop(port)"}]
[data: {:eol, "  end"}]
[data: {:eol, ""}]
[data: {:eol, "  defp loop(port) do"}]
[data: {:eol, "    receive do"}]
[data: {:eol, "      {^port, {:data, data}} ->"}]
[data: {:eol, "        IO.inspect(data: data)"}]
[data: {:eol, "        loop(port)"}]
[data: {:eol, ""}]
[data: {:eol, "      {^port, {:exit_status, exit_status}} ->"}]
[data: {:eol, "        IO.inspect(exit_status: exit_status)"}]
[data: {:eol, "    end"}]
[data: {:eol, "  end"}]
[data: {:eol, "end"}]
[data: {:eol, ""}]
[data: {:eol, "Command.exec(\"cat\", [\"port.exs\"])"}]
[exit_status: 0]
axelson

axelson

Scenic Core Team

I’d recommend looking into exexec | Hex (which wraps the erlexec Erlang library) especially if the command you want to run are not written to conform to the behavior that Port expects.

shahryarjb

shahryarjb OP

Hi dear @axelson, it has no document with sample code?

ruslandoga

ruslandoga

shahryarjb

shahryarjb OP

I have a question, where we can put env like env: [{"MIX_ENV", "#{Mix.env()}"}]?

path = System.find_executable("mix")
port = Port.open({:spawn_executable, path}, [:binary, :exit_status, args: ["deps.get"], line: 1000])
flush()
ruslandoga

ruslandoga

Port.open({:spawn_executable, path}, env: [{'MIX_ENV', 'dev'}])

Note that Mix.env() is not always available at runtime.

shahryarjb

shahryarjb OP

I have this error:

Port.open({:spawn_executable, path}, env: [{"MIX_ENV", "dev"}])
** (ArgumentError) errors were found at the given arguments:

  * 2nd argument: invalid option in list

    :erlang.open_port({:spawn_executable, "/Users/shahryar/.asdf/installs/elixir/1.13.3-otp-24/bin/mix"}, [env: [{"MIX_ENV", "dev"}]])

You put it without [:binary, :exit_status, args: ["deps.get"], line: 1000]

I even try like this:

port = Port.open({:spawn_executable, path},[:binary, :exit_status, args: ["deps.get"], env: [{"MIX_ENV", "dev"}], line: 1000])

but I have this error:

** (ArgumentError) errors were found at the given arguments:

  * 2nd argument: invalid option in list

    :erlang.open_port({:spawn_executable, "/Users/shahryar/.asdf/installs/elixir/1.13.3-otp-24/bin/mix"}, [:binary, :exit_status, {:args, ["deps.get"]}, {:env, [{"MIX_ENV", "dev"}]}, {:line, 1000}])
ruslandoga

ruslandoga

Try using charlists instead of binaries:

Port.open({:spawn_executable, path}, env: [{'MIX_ENV', 'dev'}])
shahryarjb

shahryarjb OP

Thank you for all your efforts, it works, and I need to make it comfortable with my code and idea in my project; but I think I got my needed answer, so I need to do more research and work with it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews