Is it possible to start selected apps in umbrella by using Mix?

This accepted solution is using mix cmd, which has a section in the official docs:

Beware that the Erlang VM does not terminate child processes when it shuts down. Therefore, if you use mix cmd to start long running processes and then shut down the VM, it is likely that those child processes won’t be terminated with the VM.

Because of that, I don’t think that’s a good solution.


I like @hauleth 's solution —— Don’t start any application by using --no-start, then start the required application manually.


Some contents from my notebook. 🠷

Start non-interactive shells:

$ mix run --no-start --no-halt -e "Enum.each([:a, :b], fn app -> {:ok, _} = Application.ensure_all_started(app) end)"
$ mix run --no-start --no-halt -e "Enum.each([:c], fn app -> {:ok, _} = Application.ensure_all_started(app) end)"

Start interactive shells:

$ iex -S mix run --no-start -e "Enum.each([:a, :b], fn app -> {:ok, _} = Application.ensure_all_started(app) end)"
$ iex -S mix run --no-start -e "Enum.each([:c], fn app -> {:ok, _} = Application.ensure_all_started(app) end)"

IEx will keep the BEAM instance running, so --no-halt is unnecessary in this case.

If cleaner code is required, try to move the eval code into standalone files, then above shell commands will be:

$ mix run --no-start --no-halt scripts/start-web.exs
$ mix run --no-start --no-halt scripts/start-db.exs

$ iex -S mix run --no-start scripts/start-web.exs
$ iex -S mix run --no-start scripts/start-db.exs

If you are using Phoenix, and want to start the Phoenix endpoint, remember to put following content into -e <code> or .exs file:

Application.put_env(:phoenix, :serve_endpoints, true, persistent: true)

From: phoenix/lib/mix/tasks/phx.server.ex at fd7421a9d37b126b32e29a7476256384a3ca4257 · phoenixframework/phoenix · GitHub

2 Likes