Fix Nginx 'invalid PID number' Error When Reloading or Stopping Service

intermediateโšก Nginx2026-05-16| Ubuntu 20.04/22.04, Debian 11/12, CentOS 7/8, Nginx 1.18+, systemd

Error Message

nginx: [error] invalid PID number "" in "/run/nginx.pid"
#nginx#pid#reload#systemd#service

The Error

You run nginx -s reload or systemctl reload nginx and get hit with:

nginx: [error] invalid PID number "" in "/run/nginx.pid"

Or sometimes:

nginx: [error] invalid PID number "" in "/var/run/nginx.pid"

The service might still be running, or it might be completely dead. Either way, the reload or stop command refuses to work.

What's Actually Happening

Nginx tracks its master process PID in a file (usually /run/nginx.pid). When you send a reload or stop signal, Nginx reads that file to know which process to signal. This error means one of three things:

  • The PID file exists but is empty (zero bytes)
  • The PID file contains garbage instead of a number
  • The PID file is missing entirely, but Nginx is still looking for it

Four things typically cause this: an unclean shutdown, a failed start attempt, a system reboot (which wipes the tmpfs /run directory), or a race condition during startup.

Step 1: Check What's Actually Running

Before touching anything, confirm whether Nginx is alive:

ps aux | grep nginx | grep -v grep

What you do next depends on what you find.

Scenario A: Nginx IS running

Master and worker processes are visible โ€” Nginx itself is fine. The PID file just got corrupted or wiped. Grab the real PID and rebuild the file:

# Get the master process PID
ps aux | grep 'nginx: master' | grep -v grep | awk '{print $2}'

# Write it to the PID file
sudo sh -c 'echo $(ps aux | grep "nginx: master" | grep -v grep | awk "{print \$2}") > /run/nginx.pid'

# Verify the file looks right
cat /run/nginx.pid

Then retry your reload:

sudo nginx -s reload

Scenario B: Nginx is NOT running

The PID file is a ghost. Clean it up and start fresh:

# Remove the stale PID file
sudo rm -f /run/nginx.pid

# Start Nginx
sudo systemctl start nginx

Step 2: The Reliable Fix โ€” Use systemctl

Running a systemd-based distro (Ubuntu 16+, Debian 8+, CentOS 7+)? Drop the habit of calling nginx -s reload directly for managed services. Let systemd handle it:

# Reload config (graceful, zero-downtime)
sudo systemctl reload nginx

# Or restart if reload won't cooperate
sudo systemctl restart nginx

Unlike the raw signal approach, systemctl tracks actual process state โ€” it doesn't depend solely on the PID file being correct.

Step 3: Fix the Root Cause โ€” PID File Wiped on Reboot

On Ubuntu and Debian, /run is a tmpfs. It disappears on every reboot. If Nginx fails partway through boot โ€” say, a config error or port conflict โ€” it may write the PID file and then die, leaving an empty file and no running process.

Check the startup logs:

sudo journalctl -u nginx --since "30 minutes ago" --no-pager

Config errors and port conflicts (like something already bound to port 80) show up here. Fix those first, then validate the config:

sudo nginx -t

A passing config test looks like this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Don't proceed until this passes cleanly.

Step 4: Confirm the PID File Path Matches Your Config

Nginx's config declares where the PID file lives. A mismatch between that path and what systemd expects will keep triggering this error. Check what your config says:

grep -i pid /etc/nginx/nginx.conf

Typical output:

pid /run/nginx.pid;

Verify that path exists and is writable:

# List nginx-related files under /run
ls -la /run/ | grep nginx

# If the PID file exists, it should contain a number like 12345
cat /run/nginx.pid

Some configs reference /var/run/nginx.pid instead of /run/nginx.pid. On modern systems these are usually the same symlink, but confirm:

ls -la /var/run | grep nginx
# Typically shows: /var/run -> /run

Step 5: If It Keeps Happening โ€” Fix the systemd Unit

On older distro packages, the Nginx systemd unit sometimes ships with a mismatched PIDFile directive. Check the active unit file:

sudo systemctl cat nginx

Look for these lines:

PIDFile=/run/nginx.pid
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'

If PIDFile doesn't match what's in nginx.conf, fix the mismatch. The cleanest way is a systemd override โ€” this survives package updates:

sudo systemctl edit nginx

Add the correct path:

[Service]
PIDFile=/run/nginx.pid

Then reload systemd and restart:

sudo systemctl daemon-reload
sudo systemctl restart nginx

Verify the Fix

# PID file should contain a valid number
cat /run/nginx.pid
# Expected: something like 1234

# Service should be active
sudo systemctl status nginx
# Look for: Active: active (running)

# Test reload works cleanly
sudo systemctl reload nginx
# No output = success

# Confirm workers are up
ps aux | grep nginx | grep -v grep

Quick Reference: Which Fix for Which Situation

  • PID file empty, Nginx running: Rebuild the PID file with the actual master PID, then reload
  • PID file empty, Nginx not running: Delete the PID file, run nginx -t, then start
  • Happens after every reboot: Check journalctl for startup errors โ€” something is failing during boot
  • Config path mismatch: Align the nginx.conf PID path with the systemd unit's PIDFile directive

Running as Non-Root or Inside Docker?

The Nginx process may not have write permission to /run/nginx.pid. Move the PID file somewhere the process can actually write to:

pid /tmp/nginx.pid;

Update any systemd or init scripts to match the new path. Note that this requires a full restart โ€” not a reload. Nginx only starts writing to a new PID path after a clean restart.

Related Error Notes