How to generate a Pid-File for an Elixir app

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"},

Then choose a file name

In your config.exs, add a line to specify the name of your file:

config :pid_file, file: "./"

Start your server

You should now see a file named 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:


This is what I'm doing at the moment (no guarantees it's ideal for your setup!)

# Systemd config for Campsite App
Description=Campsite daemon (Elixir web server release)


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

Back to index

No Comments