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:
-
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?
-
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!
3 Likes
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.
2 Likes
Seems useful, but also appears to have some system requirements, I.e binaries in the path etc. Igniter has to run pretty much anywhere without assumptions that anything other than Elixir is available.
Have you considered using Port
? Port — Elixir v1.17.3
edit: I should have fully read the thread 
I’m curious why you can’t use Mix.Task.run
? I feel like I have an inkling of an idea why, but I’d love to hear specifically
System.cmd
is using ports. That’s likely you get zombie processes as well, because ports don’t exit started processes, but they expect the called process to collaborate and shut down on their own when stdin is closed on them. Strange though that it wouldn’t be consistent. Probably some race condition.
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}
1 Like
So in this case its a mix archive that is creating a new application and then running a mix task in that new application.
But…maybe there actually is a way
maybe I can evaluate the mix.exs
file to “switch” to that project somehow…
yep. This appears to work. I even led in with "don’t try to talk me into using MIx.Task.run

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