Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx says the PID file is invalid, or systemd cannot read it

Errorinvalid PID number "" in "/run/nginx.pid"

Short answer

nginx and systemd both read one small file that holds the master process ID. If that file is empty, left over from a dead process, or lives at a different path than the other side expects, you get invalid PID number or Failed to read PID. Check the file's content, then make nginx.conf and the unit agree on its path.

  • Applies to nginx 1.26, systemd 255, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at what is inside the PID file and whether a master process is really running. The two answers split the causes below.

    sudo cat /run/nginx.pid; echo "---"
    ps axw -o pid,ppid,command | grep "nginx: master"
  2. 2Match what you see, then jump to its fix

    1. 1invalid PID number "" in "/run/nginx.pid" when you run nginx -s reload or nginx -s quitThe PID file is empty or stale, left behind by a process that diedFix
    2. 2Failed to read PID from file, or open() of a nginx.pid path that does not existnginx.conf and the service unit point at different PID file pathsFix

What does the error look like?

  • invalid PID number "" in "/run/nginx.pid"
  • nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
  • open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

Cause 1 of 2

The PID file is empty or stale, left behind by a process that died

1Check

nginx -s reads the file, and it only accepts a number in it. An empty file, or text that is not a number, gives the invalid PID message. If the ps line from the first step shows no master process, the file is a leftover.

sudo od -c /run/nginx.pid | head
pgrep -a nginx

2Fix

Stop any master process that is still running with a graceful QUIT signal, remove the leftover file, then start nginx through systemd so it writes a fresh one.

# only if the ps line showed a master: replace 1628 with its PID
sudo kill -s QUIT 1628
sudo rm -f /run/nginx.pid
sudo systemctl start nginx

3Verify

The file now holds one number, and a reload signal is accepted without complaint.

cat /run/nginx.pid
sudo nginx -s reload

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

nginx.conf and the service unit point at different PID file paths

1Check

The pid directive decides where nginx writes the file; the default is logs/nginx.pid inside the nginx prefix. systemd reads the path from PIDFile= in the unit. If the two differ, systemd looks where nothing was written. nginx -s also uses the pid value of whatever config it loads.

sudo nginx -T 2>/dev/null | grep -E "^\s*pid "
systemctl cat nginx | grep -i pidfile

2Fix

Make the pid directive in the main context of nginx.conf equal to PIDFile= in the unit, then restart.

# top of /etc/nginx/nginx.conf, outside http { }
pid /run/nginx.pid;

3Verify

Test the configuration, restart through systemd and confirm the unit is active.

sudo nginx -t && sudo systemctl restart nginx
systemctl status nginx --no-pager

Confirmed by freedesktop.org

Did this fix it?

How do I stop it coming back?

  • Start, stop and reload nginx only through systemctl, so a hand-started master never competes with the one systemd tracks.
  • Change the pid path in one place, then update PIDFile= to match in the same edit.
  • After changing the pid directive, restart nginx once and confirm the new file exists before you rely on reload scripts.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.