ojinari

ojinari

I compile and deploy my Phoenix projects manually, with no third-party library such distillery, and with a help of the commands “mix compile” and “mix release”, basically.

I need a way to run custom commands or tasks on a server in the production mode. It’d be similar to running migrations. But I’ve bewildered as for how it’s done. A project itself may or may not be up and running when a command or task is being executed. And I need to be able to pass some arguments to a said command-task.

How to do it?

  1. I’m aware of

defmodule Mix.Tasks.MyTask1 do .... but how would I do it in production, on a server? Is this even a proper and recommended way to run them on a server in production?

  1. I’m aware of bin/my_app eval 'MyProject.MyTask1' but why does it exist if there’s the other abovementioned approach? Again, is this one a proper and recommended way to run them on a server in production?

  2. Are there other approaches?

I’m bewildered. Which one to use, in what circumstances? How to use them properly?

Showing Posts 1 to 6

tao

tao

Your second option (/bin/app eval …) looks right. Mix is a build tool and is not a part of your release. There’s a nice example of how this can be done in the Phoenix “Deploying with releases” docs.

For passing arguments, you should be able to just pass them as arguments to the command you’re running — the mix release docs have the example of bin/RELEASE_NAME eval "IO.puts(:hello)".

ojinari

ojinari OP

That doesn’t answer the question of “how to pass an argument to my custom function”

bin/RELEASE_NAME eval "IO.puts(:hello)"

because it passes some code to eval

For instance:

``

defmodule MyApp.Release do
  @app :my_app

  def abc(a1, b1, c1) do
    # [.......]
  end

How to pass a1, b1, c1 to abc() in

$ _build/prod/rel/my_app/bin/my_app eval "MyApp.Release.abc ???"

?

John-Goff

John-Goff

Just pass them? Eg

$ _build/prod/rel/my_app/bin/my_app eval "MyApp.Release.abc(1, \"str\", :atom)"

You’re basically calling a function the same way as you would in IEx, so you can pass any elixir data type.

gpreston

gpreston

Add-on to this which is not super clear from this section of the docs:

I was trying to forward command-line arguments directly to my Elixir code being run. Doing it the naive way doesn’t work for arbitrary unexpected numbers of arguments and other reasons:

$ _build/prod/rel/my_app/bin/my_app eval "MyApp.Release.abc(\"$1\", \"$2\")" # this doesn't even properly escape double quotes, and ways to do that within bash are not consistent across systems

There is a supported way to forward the command line arguments though:

$ _build/prod/rel/my_app/bin/my_app eval "MyApp.Release.abc" "$@"

The command line arguments can then be accessed in your Elixir code using System.argv(). Hope this helps anyone facing the same problem. This seems like a better solution to me for anything more complex than passing hard-coded values from the shell script.

rugyoga

rugyoga

If you’ve deployed your app using releases, it’s straightforward to fire up iex:

$ _build/prod/rel/my_app/bin/my_app remote
iex(1)> MyProject.Module.method(1, 2, 3)
BartOtten

BartOtten

Have you considered contributing the improvements to the official documentation?

— All posts loaded —

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
jtormey
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews