itopiz

itopiz

How to use mix release's `env.bat` with Windows service?

Hi,

I have an Elixir 1.15.7 application. At first, it was intended to be installed only on Windows. Therefore I configured mix.iex with Windows-specific environment variable ALLUSERSPROFILE in my config provider:

defmodule MyApp.MixProject do
  use Mix.Project

  def project do
    [
      ...
      releases: releases(),
    ]
  end

  ...

  defp releases do
    [
      myapp: [
        include_executables_for: [:windows],
        config_providers: [
          {MyApp.Config.TOMLConfigProvider,
           "%ALLUSERSPROFILE%\\MyCompany\\MyApp\\config.toml"}
        ]
      ]
    ]
  end
end

Now, I need to provide releases for linux as well and I first would like to get rid of the Windows-specific environment variable in mix.iex.

I have adapted my config provider to be used like so:

defp releases do
  [
    myapp: [
      include_executables_for: [:unix, :windows],
      config_providers: [
        {MyApp.Config.TOMLConfigProvider,
         {:system, "MYAPP_CONFIG_DIR", "/configuration.toml"}}
      ]
    ]
  ]
end

and I have created a rel/env.bat.eex:

@echo off

set MYAPP_CONFIG_DIR=%ALLUSERSPROFILE%\MyCompany\MyApp

With that in place, I can successfully launch my release with:

C:\MyCompany\MyApp>dir
 Volume in drive C is OS
 Volume Serial Number is ************

 Directory of C:\MyCompany\MyApp

17.01.2024  15:34    <DIR>          .
17.01.2024  15:01    <DIR>          ..
17.01.2024  15:33    <DIR>          bin
17.01.2024  15:33    <DIR>          erts-13.2.2.5
17.01.2024  15:34    <DIR>          lib
17.01.2024  15:34    <DIR>          releases
17.01.2024  15:34           451 643 unins000.dat
17.01.2024  15:33         3 223 613 unins000.exe
               2 File(s)      3 675 256 bytes
               6 Dir(s)  1 530 448 859 136 bytes free

C:\MyCompany\MyApp>bin\myapp.bat start

But the goal is to install the Windows service and after doing so (i.e. bin\myapp.bat install), the problem is that the service stops immediately after it started:

C:\MyCompany\MyApp>erts-13.2.2.5\bin\erlsrv.exe start myapp_myapp
erts-13.2.2.5\bin\erlsrv.exe: Service myapp_myapp started.

I am pretty sure the env.bat is not executed when starting the Windows service. I have no log to confirm this but when I use an absolute path in my config provider instead of the {:system, "MYAPP_CONFIG_DIR", "/configuration.toml"} tuple, then the service starts and I can successfully access my web app.

Is mix release’s env.bat suppose to work with Windows service?

Most Liked

cmo

cmo

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New

We're in Beta

About us Mission Statement