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 · GitHub

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.

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

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement