Attach to a running Elixir instance

To be able to attach to a running Elixir instance, the process first needs to have a name set to refer to.1

For example, a Phoenix application is usually started by running the mix phx.server command. Instead, start it through the elixir executable via the -S flag.2 The command also includes the --sname flag (for short name), which is used to refer to the process later.3

elixir --sname phoenix -S mix phx.server

Then, to connect to the node, use the --remsh (for remote shell) command to refer to the running phoenix process:

iex --sname console --remsh phoenix

  1. https://elixir-lang.org/getting-started/mix-otp/distributed-tasks.html#our-first-distributed-code ↩︎
  2. The -S flag executes the given script in path, and can be used instead of running the script directly through both the elixir and iex executables. The following commands are equivalent:

    mix phx.server
    
    elixir -S mix phx.server
    

    Running a command through the elixir executable is useful, becauase it takes more arguments, like the --sname one described above.

    The iex executable also takes an -S flag. To start the Phoenix server with an iEX session attached, run the mix phx.server command through iex:

    iex -S mix phx.server
    
    ↩︎
  3. https://www.erlang.org/doc/reference_manual/distributed.html ↩︎