Process IDs are very important in Unix. It's standard for services to create a file called a "Pid-File" that has the ID of its currently running process so that other applications can discover the PID of the service and manage it. Management tools such as Systemd make heavy use of Pid-Files.
If you're deploying a Phoenix app or other Elixir-based service to a Unix/LInux OS, there's a good chance you'll want to have it generate a Pid-File at some point. Fortunately, there's a drop-in utility library, pid_file.ex that makes this simple!
Add pid_file. to your mix.exs
defp deps do
[
# ...your other deps
{:pid_file, "~> 0.1.0"},
]
end
Then choose a file name
In your config.exs
, add a line to specify the name of your file:
config :pid_file, file: "./my_app.pid"
Start your server
You should now see a file named my_app.pid
at the root directory of your application that contains a single number, which is your app's process ID. E.g. "60696".
You can then kill your application by killing whatever PID was listed inside this file. E.g. kill 60696
Use this file location (or a link to it) in your system config
Now that your Elixir app has a discoverable PID, other tools can manage it. How you do this depends on your particular system and setup.
If you're using Systemd, for example, you would add the following line to the [Service]
block in your unit:
PIDFile=/some_path/your_app/your_app.pid
This is what I'm doing at the moment (no guarantees it's ideal for your setup!)
# Systemd config for Campsite App
[Unit]
Description=Campsite daemon (Elixir web server release)
After=network.target
[Service]
Type=forking
User=primary
Group=primary
Restart=always
RestartSec=5
Environment=MIX_ENV=prod
Environment=LANG=en_US.UTF-8
Environment=PORT=4002
SyslogIdentifier=campsite
PIDFile=/home/primary/releases/campsite/campsite.pid
WorkingDirectory=/home/primary/builds
ExecStart=/home/primary/releases/campsite/bin/campsite start
ExecStop=/home/primary/releases/campsite/bin/campsite stop
ExecStop=/home/primary/releases/campsite/bin/campsite reload_config
[Install]
WantedBy=multi-user.target
No Comments