OvermindDL1

OvermindDL1

PidFile - create and manage a PID file from the BEAM process

I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM.

The reason you might want this it to make a ‘proper’ systemd management file or something, or just an easy way to identify the PID’s of your multiple BEAM processes (I have a lot of BEAM instances running for example, this is useful to figure out which is which).

It’s Hex URL: pid_file | Hex

It’s README.md:

PidFile

Manages a simple OS PID file for this BEAM system.

In other words it just makes a file whose sole contents is the Operating System PID of the running BEAM process.

It should also auto-clean old PID files on load, and clear the PID file on a ‘proper’ shutdown, but even if not a proper shutdown then it will still clear it properly next time.

Hex: pid_file | Hex

Installation

    {:pid_file, "~> 0.1.0"},

Setup

Global Config

Add one of these to your config for it to be managed globally, replacing the values as necessary:

config :pid_file, file: "./my_app.pid"
config :pid_file, file: {:SYSTEM, "PIDFILE"}

Locally Managed

Add the worker to your supervision tree:

worker(PidFile.Worker, [[file: "/run/my_app.pid"]])

Most Liked

OvermindDL1

OvermindDL1

Also, if it helps, I use this overlay to generate (via distillary) a systemd service file using this PID library:

[Unit]
Description=<%= description %>
After=network.target

[Service]<% full_dir = Path.absname(output_dir)%>
Type=forking
User=<%= deploy_user %>
Group=<%= deploy_group %>
WorkingDirectory=<%= full_dir %>
ExecStart=<%= full_dir %>/bin/<%= p_name %> start
ExecReload=<%= full_dir %>/bin/<%= p_name %> reload_config
ExecStop=<%= full_dir %>/bin/<%= p_name %> stop
PIDFile=<%= full_dir %>/<%= p_name %>.prod.pid
Restart=always
RestartSec=5
Environment=PORT=3000
Environment=LANG=en_US.UTF-8
SyslogIdentifier=<%= p_name %>

[Install]
WantedBy=multi-user.target

Been using it in production ever since migrated the server from Windows 2008 to redhat, so a couple weeks now without issue. This follows the proper systemd model using a PID file with proper restarts (tested it by manually telling the server to stop, killing it, kill -9’ing it, comes back up in 5 seconds every time).

OvermindDL1

OvermindDL1

Well first of all that should not be there. That means ‘pretend everything stays working even when the application closes’. Why do you have that there?! o.O

aboroska

aboroska

Probably it is worth mentioning in this thread that it is common to supervise the VM via the built-in heart mechanism: http://erlang.org/doc/man/heart.html

It works by starting a separate process called heart that not only checks if the beam process is still alive but also sends heartbeat messages to it (hence the name).

Of course if you use heart other restart mechanisms should not be in place. Also if you want to manually kill the VM you need to kill the heart process first, then the VM. Better option to stop the VM would be to call init:stop() from a remote node as described here for example: http://erlang.org/pipermail/erlang-questions/2010-August/052957.html

The main advantage of the heart method is that it is OS independent.

OvermindDL1

OvermindDL1

Via an overlay template. :slight_smile:

Essentially I just have these added to be release ... do ... end block:

  set overlay_vars: [
        deploy_user: user,
        deploy_group: group
      ]

  set overlays: [
        {:template, "priv/templates/my_server.service.eex", "my_server.service"}
      ]

Still using it to this day. ^.^

brucepomeroy

brucepomeroy

Nice. Thanks for your help @OvermindDL1. That was my problem. Removed that line and it’s working :grin:

I had it in there because i saw in the Distillery docs (https://hexdocs.pm/distillery/use-with-systemd.html#content) that “It’s important that you have RemainAfterExit=yes set, or you will get an error trying to start the service.” I didn’t give any thought to the effect that line would have. And contrary to the note in the docs I haven’t had trouble starting the service since removing that line.

Thanks again!

Where Next?

Popular in Libraries Top

alisinabh
Hey everyone i’ve developed a library for Jalaali calendar for elixir which supports converting Gregorian dates to Jalaali and vice vers...
New
wmnnd
Hi there, for my project DBLSQD, I needed a file storage solution that is a bit more flexible than Arc. Because I thought others might f...
New
dbern
I’m excited to announce that TaxJar has developed and open-sourced DateTimeParser. We developed it because we found a need to parse user ...
New
sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
michalmuskala
Hello everybody. I have just released Jason - a new JSON library. You might be wondering, why do we need a new library? The primary foc...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
Crowdhailer
I have been updating a library that allows you to pipe between functions that use the erlang result tuple convention. Assuming you have...
New
gabrielpoca
Hello everyone! I want to share with you something that I’m really proud of: https://stillstatic.io/ Still is a static site builder for...
New
bryanjos
Hi, I wanted share a small library we at Revelry Labs made for rendering react components from the server side. There are instructions fo...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Sub Categories:

We're in Beta

About us Mission Statement