How can I run a `mix run priv/foo.exs` without starting Cowboy and other genservers?

mix
phoenix

#1

Hi,

subject says everything: how can I do a mix run without starting the webserver and a selection of other gen servers? Background: I have a maintenance.exs I want to run every night, but currently it crashes on startup because it tries to an interface already in use.

Best regards,
CK


#2

Hey :slight_smile:

From mix help run:

--no-start - does not start applications after compilation

…so in your case:

mix run --no-start priv/maintenance.exs

Regards,
Johan


#3

Hey!

oh, I missed that. Thanks!

Best regards,
CK


#4

OK, follow-up question: how do I start Ecto when running a script with --no-start? :wink:


#5

Like this:

repos = parse_repo([])
Enum.each(repos, fn repo ->
  ensure_repo(repo, [])
  {:ok, _pid, _apps} = ensure_started(repo, [])
end)