zachdaniel

zachdaniel

Creator of Ash

Calling `System.cmd` in such a way that stdin and stdout are connected, and they halt together

Hey everyone!

One of my mix tasks needs to use System.cmd. It is technically calling another mix task, but Mix.Task.run is not on the table (for a whole bunch of reasons that I don’t think are worth getting into right now).

However, this cmd I’m calling may prompt for input potentially. By default System.cmd does not forward stdin from the elixir process to the the System.cmd process. I’m not the most familiar with how all this works, only a layman’s knowledge of linux stdio piping etc.

So, I’ve found an option to System.cmd that seems like it should do what I need, but it has its own quirks and problems that make it effectively unusable as far as I can tell. The option is use_stdio: false.

I’ve got an example project here: GitHub - zachdaniel/mix-task-example

Warning: this is broken and will start printing numbers to your terminal in such a way that you can’t stop without exiting that terminal window. Should be harmless, but just want to make it clear.

To reproduce the behavior I’m seeing, run mix example.run and enter a number. It will start counting up.

There are two problems:

  1. it seems to just occasionally “miss” the input? i.e sometimes I type a number, hit enter, and its like nothing happened? I press enter again, and then it’s like I provided an empty string to the input. As if there was some “lag time” before it is accepting input?

  2. Most importantly (well, they are both important), but it seems to leave zombie processes running after pressing Ctrl+C to quit the process.

From what I can tell, ports are a way to do this sort of thing, and if I must go down that route I will, but it seems like a very low level API w/ lots of fiddly bits, so I’m looking for simpler alternatives.

I’d also prefer not to reach for an external library, but I can if I must. This is for the igniter package, and I want to keep its footprint light.

Thanks in advance!

Most Liked

TwistingTwists

TwistingTwists

Had a look at muontrap? That lib solves for zombie process.
https://hexdocs.pm/muontrap/MuonTrap.html

I use it for driving long running ffmpeg commands.

zachdaniel

zachdaniel

Creator of Ash

yep. This appears to work. I even led in with "don’t try to talk me into using MIx.Task.run :laughing:

Anyway, this is far better than having to shell out, and appears to play nice in every way I need it to.

jstimps

jstimps

GitHub - erlexec is great and might have what you need.

An example from the Readme.

% Execute an OS process (script) that reads STDIN and echoes it back to Erlang
25> f(I), {ok, _, I} = exec:run("read x; echo \"Got: $x\"", [stdin, stdout, monitor]).
{ok,<0.427.0>,26431}
% Send the OS process some data via its stdin
26> exec:send(I, <<"Test data\n">>).                                                  
ok
% Get the response written to processes stdout
27> f(M), receive M -> M after 10000 -> timeout end.
{stdout,26431,<<"Got: Test data\n">>}
% Confirm that the process exited
28> f(M), receive M -> M after 10000 -> timeout end.
{'DOWN',26431,process,<0.427.0>,normal}

Where Next?

Popular in Questions Top

quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement