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
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
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
Probably it is worth mentioning in this thread that it is common to supervise the VM via the built-in heart mechanism: heart — OTP 29.0.2 (kernel 11.0.2)
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: [erlang-questions] How to stop a detached OTP app?
The main advantage of the heart method is that it is OS independent.
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









