zachdaniel

zachdaniel

Creator of Ash

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!

Showing Posts 1 to 8

TwistingTwists

TwistingTwists

Had a look at muontrap? That lib solves for zombie process.

I use it for driving long running ffmpeg commands.

zachdaniel

zachdaniel OP

Creator of Ash

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.

andyleclair

andyleclair

Have you considered using Port? Port — Elixir v1.20.2

edit: I should have fully read the thread :man_facepalming:

andyleclair

andyleclair

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

LostKobrakai

LostKobrakai

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.

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}
zachdaniel

zachdaniel OP

Creator of Ash

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 :laughing: maybe I can evaluate the mix.exs file to “switch” to that project somehow…

zachdaniel

zachdaniel OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews