cjbottaro

cjbottaro

How to use System.cmd’s :into option with /dev/null?

Hello…

How to use System.cmd’s :into option with /dev/null?

Something like…

System.cmd("foo", [], into: IO.stream("/dev/null", :line))

Thanks for the help!

Marked As Solved

voltone

voltone

If you want to avoid having Elixir reading the output altogether, then you’re going to have to have the OS do the redirection for you. You can do that running the command in a shell, e.g. System.cmd("sh", ["-c", "foo >/dev/null"]). But this is only really safe if the command and its arguments are fully under your control. If there is any user input involved, this approach can make your application vulnerable to command injection! More about that here.

If instead you just want Elixir to consume the output chunks and not collect them in memory, writing to an IO stream is not going to be very efficient. Instead you could implement a null collectable like this:

defmodule DevNull do
  defstruct []

  defimpl Collectable do
    def into(original) do        
      collector_fun = fn         
        _dev_null, :halt -> :ok  
        dev_null, _ -> dev_null  
      end
      {original, collector_fun}
    end
  end
end

System.cmd("foo", [], into: %DevNull{})

But that’s only really needed for commands that produce a lot of output. In most cases I would skip :into altogether and just ignore the first element of the return value of System.cmd/2.

Where Next?

Popular in Questions Top

New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement