Basics

What is a process PID — and why kill on an old number can kill someone else's program

Illustration: a huge cloakroom of hooks and tokens; one hook is empty, its token still swinging

Here's a sneaky one. You start a server, write its number to server.pid, and a week later you run kill $(cat server.pid). The server crashed long ago. But the command succeeded — and something closed.

What, exactly? Some other program the system handed the same number to. Process numbers get reused. Let's see how they work and how not to kill the wrong thing.

What a PID is, in plain words

A PID (process ID) is the number the system gives every running program. You use it to talk to the process: stop it, check on it, see how much it eats.

Picture a cloakroom. You hand in your coat and get a token. While the coat hangs there, the token is yours. Take the coat back — the token returns to the counter and goes to the next visitor. PIDs work exactly like that: the number belongs to a process while it's alive.

The same program launched twice is two processes with two different PIDs. Open three terminals — each has its own number.

How to find a PID

A few ways you'll use every day:

  • echo $$ — the number of your current shell, the one you're typing in.
  • sleep 100 & and right away echo $! — the number of the last program you started in the background.
  • pgrep -f server.js — find a process by a piece of its command.
  • lsof -i :3000 — who's holding port 3000. This is the go-to move when the port is already in use.
  • ps aux or top — the whole list, numbers in the PID column.

Then the number goes into kill: kill 4821 politely asks the process to exit. If kill doesn't help, the process may have already become a zombie — then it's the parent you need to fix.

Why a number can end up with someone else

Linux hands out numbers in order: 4821, 4822, 4823… When it hits the ceiling, it wraps around and takes free ones.

The ceiling lives in /proc/sys/kernel/pid_max. On our server it's 4,194,304. On older systems it can be 32,768 — and a busy day can cycle through all of them. On macOS the ceiling is also around a hundred thousand, not millions.

Hence the .pid file trap. The number in the file is a token from the past. If the process died and the server rebooted, that same number can easily belong to anything: a database, someone's script, your editor.

How to stay safe:

  1. Before kill, check who it is: ps -p $(cat server.pid) -o comm=. If it's not your node — hands off.
  2. Better still, don't keep numbers by hand. For long-running programs there's systemd or pm2 — they track their own processes. More on that in the article about daemons.
  3. For a "kill what I just started" script, use $! right after launching, not a number from last week.

PID 1 is special

The very first process on the system gets number 1. On ordinary Linux that's systemd. It starts everything else and adopts "orphaned" processes whose parent died.

PID 1 has an odd rule: signals it hasn't set up a handler for are simply ignored by the kernel. For a normal program, kill by default means "die". For the program with number 1 — nothing.

Where this bites beginners: in Docker. Inside a container your node server.js often ends up as PID 1. You press Ctrl+C or run docker stop — and the container hangs for ten seconds until Docker kills it by force. The fix is the docker run --init flag: number 1 goes to a tiny helper, and your program gets a normal number and reacts to signals properly.

Where you'll meet a PID

  • In error messages: Error: listen EADDRINUSE — and lsof shows the culprit's PID.
  • In logs: many programs write their number on every line so you can tell two parallel runs apart.
  • With AI agents. Claude Code or Codex start a dev server in the background and later stop it by number. If the agent "forgot" the server, find it with pgrep and stop it yourself.

Can two processes have the same PID?

Not at the same time — a number is unique among live processes. But one after another — yes: once a process dies, its number frees up and can go to a new one.

What's the difference between kill and kill -9?

Plain kill asks the program to exit, and it gets time to save its data. kill -9 cuts it off instantly, with no chance to clean up — that's the last resort.

Can I check whether a process is alive without killing it?

Yes: kill -0 4821. It sends nothing and only checks. If the process is alive and it's yours, the exit code is 0.

In KODiQ, things like this are explained in five minutes, with real-life examples and no memorizing commands.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →