Skip to main content

PID

A PID (process ID) is the unique integer the Linux kernel assigns to each running process. PID 1 is the first process after boot, normally systemd, and every other process descends from it.

Understanding PIDs in Linux

What Is a PID?

A PID is a process's address. When you want to stop one nginx worker and not all nginx processes, you need its PID. The kernel hands out PIDs as processes are created, and numbers can be reused after a process ends, so a PID you saw yesterday may belong to something else today.

How PIDs Form a Process Tree

Every process has a parent. PID 1 is the root, and when a parent dies its children are re-parented to PID 1 (or a designated subreaper), which reaps them so they do not become zombies.

◈ DIAGRAM
PID 1 (systemd)
+-- 892 sshd
| +-- 1203 bash your SSH session
+-- 1100 nginx master
| +-- 1101 nginx worker
| +-- 1102 nginx worker
+-- 1350 node payment-service

How PIDs Work Inside Containers

Each container gets its own PID namespace, so its main process is PID 1 inside the container while having a different PID on the host.

Bash
docker exec -it mycontainer ps aux
# PID 1 node server.js inside the container
ps aux | grep 'node server.js'
# PID 8234 same process seen from the host

PID 1 in a container has special duties: it must handle SIGTERM and reap orphaned children. The kernel does not apply default signal actions to PID 1, so a signal with no handler is ignored. Use docker run --init or tini to fix both problems without changing app code.

Practical PID Commands

Bash
pidof nginx # PIDs by program name
pgrep -a nginx # PIDs with command lines
pstree -p # process tree with PIDs
echo $$ # PID of the current shell
sleep 100 & echo $! # PID of last background job
kill -HUP "$(cat /var/run/nginx.pid)" # signal via a PID file
pkill nginx # signal by name
cat /proc/sys/kernel/pid_max # upper limit on this system
cat /proc/1100/status # details, including PPid

Troubleshooting PIDs

Symptom Command What to Look For
Need a PID fast pgrep -a name PID plus command
Stale PID file kill -0 $(cat file) PID not running
Orphaned children pstree -p Re-parented to PID 1
PID differs in container ps aux inside and outside Namespace mapping
Tip

If a service refuses to start with "already running", check whether its PID file points at a live process with kill -0 $(cat /var/run/service.pid). A stale file from a crash is a common cause.

Common Mistake

Running an app directly as PID 1 in a container with no init. SIGTERM can be ignored, so docker stop waits out the timeout and sends SIGKILL.

Remember

PID 1 is special. Killing it from inside its own namespace is restricted, and killing the host's PID 1 brings the system down.

Frequently Asked Questions

Why does PID 1 matter so much in containers?

It must reap zombie children and handle signals itself, because the kernel does not apply default signal behavior to PID 1. An app without that logic can ignore SIGTERM and leak zombies, which a small init such as tini avoids.

Can a PID be reused?

Yes. PIDs are recycled after processes exit, so scripts that store a PID should confirm it still belongs to the expected process before sending signals.

What is the maximum PID value?

It is set by `/proc/sys/kernel/pid_max`. The kernel default has been 32768, and many 64-bit systemd systems raise it much higher, so check your own machine.

Found a mistake, an outdated step or a broken link?