liveBook · Manning

Mix run

As explained in Elixir in Action, Third Edition

mix run is a Mix command that runs Elixir code within a Mix project.

mix run executes Elixir code in a Mix project, loading compiled modules and dependencies without opening an interactive shell. Use it for one-off expressions, short-lived tools, or continuously running OTP applications. section 13.1.1 section 2.7.2

Choose the execution mode based on the system’s lifetime:

  • Run an expression with `mix run -e`, such as invoking `MyProject.hello()`, then exit. section 13.1.2 section 2.7.2
  • Keep an OTP application alive with `mix run --no-halt`; unlike `iex -S mix`, this starts no interactive shell. section 13.1.1
  • Use an `.exs` script for a small tool, but move complex or continuously running code into a Mix project and OTP application. section 13.1.2

Start a production-style application

Run `$ mix run --no-halt` to start the BEAM and the OTP application with its dependencies. For background operation, combine Mix with detached Erlang startup and a named node; later connect with a remote shell or stop the node through `System.stop/0`. section 13.1.1

$ mix run --no-halt          #1
 
Starting database worker.
Starting database worker.
Starting database worker.
Starting to-do cache.

Shows the observable startup output produced when `mix run --no-halt` launches application workers. section 13.1.1

How Elixir in Action, Third Edition covers it

How deep it goes
Explains expressions, runner modules, scripts, dependencies, and application lifetime, but defers detailed Mix configuration.
Examples
Uses `mix run -e`, `mix run --no-halt`, JSON-decoding scripts, runner modules, and a continuously running to-do system.
What it assumes
Assumes basic Elixir code and Mix projects while demonstrating project creation, compilation, dependency handling, and application startup.
What you can do after
Operate/configure Elixir tools and OTP applications using short-lived, persistent, detached, or remotely controlled execution modes.
How it connects
Connects Mix projects with OTP applications, BEAM startup, dependencies, scripts, remote shells, and escripts.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.