shahryarjb

shahryarjb

How to send line by line of System.cmd to LiveView when a task is running

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

Marked As Solved

ruslandoga

ruslandoga

Try using charlists instead of binaries:

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

Also Liked

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]
ruslandoga

ruslandoga

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.

Last Post!

shahryarjb

shahryarjb

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.

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement