zachdaniel
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:
-
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!
Most Liked
TwistingTwists
Had a look at muontrap? That lib solves for zombie process.
I use it for driving long running ffmpeg commands.
zachdaniel
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.
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}
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









