Prevent application start when running from iex

Hey there, in my phoenix application, I start a bunch of sub applications to perform various tasks, like listening to ActiveMQ channels.

I don’t want these applications to be started when running iex -S mix or when running my tests. I only want them to start when I run mix phx.server.

I already figured out a hack, but I’d like to learn the right way to do it. The hack : in application.ex, I use the following function to decide what I will pass as children to the Supervisor.

  defp in_phoenix? do
    {output, _} = System.cmd("ps", ["wwwp", to_string(:os.getpid), "-o", "command"])
    String.contains?(output, "phx.server")

This is what makes mix phx.server start it’s server:

So you can simply check for that configuration if phoenix is starting it’s servers or not.


Looks neat! Will try it right now

Working as expected, thanks for answering that fast! My function is now :

  defp in_phoenix? do
    Application.get_env(:phoenix, :serve_endpoints)
1 Like