A PID file, also written pidfile, is a small text file in which a running program, usually a daemon, stores its own process ID so that other tools can locate it later. In the context of services & processes on servers, the PID file is the traditional link between a long-running background service and the scripts that start, stop, reload or check it.
The file typically lives under /run or /var/run and contains a single decimal number followed by a newline, such as 1842. It is simple and portable, but it is also fragile: if the process dies without cleaning up, the file can point to the wrong process, which is why modern process supervisors rely less on it.
How a PID file is created and used
A daemon writes its PID file after it has forked into the background, because only then is the final process ID known. A classic Unix daemon forks, lets the parent exit, creates a new session, optionally forks again and then writes the PID of the surviving child to the configured path, for example /run/nginx.pid.
Other programs then read the number and act on it. An init script sends SIGTERM to that PID to stop the service, SIGHUP to make it reload its configuration, or signal 0 to test whether the process still exists. Log rotation tools use the same file to tell a daemon to reopen its log files after rotation.
The following table lists the usual stages in the life of a PID file and what happens at each one.
| Stage | Action | Responsible component |
|---|---|---|
| Startup | Daemon writes its PID to the file | The daemon itself |
| Running | File is read to send signals | Init scripts, log rotation, monitoring |
| Reload | SIGHUP sent to the stored PID | Administrator or service manager |
| Shutdown | Daemon removes the file before exit | The daemon itself |
| Crash | File stays behind as a stale entry | Nobody, unless cleanup logic exists |
Typical locations and file format
The Filesystem Hierarchy Standard places runtime PID files in /run, which on most current Linux distributions is a tmpfs mounted in memory and therefore emptied at every boot. The older path /var/run is usually a symbolic link to /run, kept for compatibility.
| Location | Typical use | Notes |
|---|---|---|
| /run/service.pid | System daemons running as root | Cleared at boot, current standard |
| /var/run/service.pid | Legacy configurations | Usually a link to /run |
| /run/service/service.pid | Daemons that drop privileges | Subdirectory owned by the service user |
| $XDG_RUNTIME_DIR | Per-user services | Private to the logged-in user |
| Application directory | Custom or development setups | Survives reboots, risk of stale files |
The content is deliberately minimal: one PID as ASCII digits, optionally followed by a newline. Readers should tolerate the trailing newline and reject anything that is not a positive integer, because a corrupted or empty file must never lead to a signal being sent to process 0 or to an unrelated group.
The stale PID file problem
A stale PID file is one that remains after its process has ended, most often because of a crash, a SIGKILL or a power loss. Because the kernel reuses process IDs, the number in the file may later belong to an entirely different program.
This creates two real risks. A start script may refuse to start the service because it believes an instance is already running, and a stop script may send a signal to the wrong process. Robust tooling therefore does not trust the number alone; it also verifies that the process with that PID has the expected executable name or command line, and it treats a missing process as a stale file to be removed.
| Symptom | Likely cause | Fix |
|---|---|---|
| Service reports already running but is down | Stale PID file after a crash | Verify the process, then remove the file |
| Stop command kills an unrelated process | PID reused by another program | Check executable name before signaling |
| Daemon cannot write its PID file | Wrong permissions on the directory | Create a subdirectory owned by the service user |
| Two instances run at the same time | No lock held on the PID file | Use an exclusive lock with the file |
A common hardening technique is to keep an exclusive advisory lock on the PID file with flock or fcntl for the whole lifetime of the process. Because the kernel releases the lock automatically when the process exits, a second instance can test the lock instead of trusting the file contents, which prevents duplicate instances reliably.
PID files compared with modern process supervision
Modern process supervisors track children directly, so they need a PID file far less often. systemd places every service in its own control group and knows all of its processes; Supervisord, PM2 and Monit keep the service as a child or poll it, and therefore receive the exit status without reading any file.
PID files still matter in several places. A systemd unit with Type=forking uses the PIDFile= option to learn which process is the main process of a traditional daemon, and many monitoring tools, log rotation configurations and legacy scripts still read them. For new services, the recommended pattern is to run in the foreground and let the supervisor own the process, which removes the PID file problem entirely.
- Foreground services under systemd or a container runtime usually need no PID file at all.
- Forking daemons should keep a PID file and declare it to the service manager.
- Containers run the main process as PID 1 inside their namespace, so a PID file inside the container is rarely useful outside it.
Good practice for writing and reading PID files
Services that must create a PID file should write it atomically, for example by writing to a temporary file in the same directory and renaming it, so that readers never see a half-written number. They should also remove the file on clean shutdown and avoid world-writable directories, because an attacker who can replace the file can redirect signals sent by root.
Scripts that read PID files should check that the process exists and matches the service before signaling it, wait for the process to exit after SIGTERM instead of assuming success, and escalate to SIGKILL only after a defined timeout as part of a graceful shutdown procedure.
A plain-language summary
Every program on a server gets a number from the operating system. It works like a ticket number at a counter. A PID file is a tiny note where a background program writes down its own ticket number when it starts.
Later, when an administrator wants to stop or restart that program, the tools read the note to know which program to talk to. The note has one weak spot. It can be left behind if the program crashes. The old number may then belong to someone else, so careful tools double-check before acting.
An example from practice
A hosting provider runs a legacy mail daemon that forks into the background and writes its PID file to /run/maild/maild.pid. After an unexpected power loss on one server, the machine reboots, but the administrators had configured the PID file in the application directory instead of /run, so the old file survives and the init script reports that the service is already running while no mail is delivered.
The team moves the PID file into /run/maild, which is cleared at every boot, declares the path with PIDFile= in a systemd unit using Type=forking, and adds a check that compares the process name before any signal is sent. After the change, a forced reboot test shows the service starting cleanly within seconds, and nightly log rotation continues to reload the daemon through the same PID file without errors.