OvermindDL1
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"]])
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
Also, if it helps, I use this overlay to generate (via distillary) a systemd service file using this PID library:
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).
brucepomeroy
Thanks for this! I’ve been trying to get systemd to restart my app if it crashes. I used your
:pid_filepackage and it seems to be managing the pidfile correctly. I added the path to the pidfile to my systemd unit file. I can start and stop the app via systemd and the app starts automatically when the server starts but if Ikillthe app it doesn’t restart. Wondering if you have any suggestions on this. I start my app using systemd, then check the pidfile to get the pid (in this case it was 1645). then runkill 1645the app stops and is not restarted. I checked the systemd logs usingjournalctl -u my_app.serviceand got the following output. Any thoughts? Thanks again!I noticed in the last line of the log messages
run_erl[1644], note 1644 is the app’s pid - 1. I also noticed the message “Supervising process 1645 which is not our child. We’ll most likely not notice when it exits”. I think this may be a clue. Isrun_erla process that “wraps” the app process? Maybe systemd interacts withrun_erlrather than our app directly and therefore doesn’t notice if the app exits.OvermindDL1
Hmm I just
kill -9 beam.smphere and it started back up after 5 seconds. Though I use the systemd service config file I displayed above.Can you post the systemd service config file that you are using? And I bet the run_erl is pre-forking or something, as you can see I’m calling the release scripts directly.
brucepomeroy
Thanks for this. Yea I’m not too sure what the issue is. My server is ubuntu so I couldn’t use the exact command you used to kill it but I tried
pkill -9 beam.smpand got the same results. Systemd reported that “Erlang closed the connection” and didn’t appear to attempt a restart. This is my conf file. Maybe I messed something up.Thanks again!
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
brucepomeroy
Nice. Thanks for your help @OvermindDL1. That was my problem. Removed that line and it’s working
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!
OvermindDL1
Distillery does it wrong, I don’t know why they don’t do it right… ^.^;
Using Distillery’s instructions your service will not get restarted, which seems to defeat the point to me? ^.^
You are welcome! Nice to see others are using this too.
Eiji
@OvermindDL1: Just looked at your code - it’s simple and useful.
I don’t understand why your are calling:
i.e.
update_pid/1callsget_pid/0that returns integer (from string) and you are changing it back to string. I could understand if it’s used in another function, but it’s not, so I’m little curious why you did it.OvermindDL1
So yeah, I needed to ensure it was an integer at one point but don’t recall where, this code is old… >.>
aboroska
Probably it is worth mentioning in this thread that it is common to supervise the VM via the built-in
heartmechanism: heart — OTP 29.0.2 (kernel 11.0.2)It works by starting a separate process called
heartthat 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
heartmethod is that it is OS independent.