Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท process

Node.js process.pid and process.ppid

Reference for process.pid and process.ppid in Node.js: the current and parent process identifiers, how children see them, and how to use them with signals.

process.pid is the process identifier (PID) the operating system assigned to the running Node.js process. process.ppid is the PID of its parent, the process that started it, such as your shell or a process manager.

PIDs are small integers that identify a process while it runs. They are useful for log lines, lock or PID files, and for sending signals with process.kill(). They are not unique over time: after a process ends, the system may reuse its number.

NODE
console.log(process.pid, process.ppid);

Try it

Node.js Reading the identifiers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Parent and child see each other

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
process.pid: number
process.ppid: number

Returns: integers, read-only.

Property Description
process.pid PID of the current process. Stable for its whole life.
process.ppid PID of the parent when process.ppid is read. It can change if the parent exits (on POSIX systems the process is adopted by init or a subreaper).

Edge cases: inside a container the PID is the number in the container's namespace, often 1 for the main process. Worker threads share the PID of the main thread; use threadId from node:worker_threads to tell them apart.

Usage notes

  • Not secure identifiers. PIDs are guessable and reused; do not use them as random tokens or session IDs.
  • PID files are a classic use: write process.pid at startup and delete the file on exit, and check with process.kill(pid, 0) whether an older instance is still alive.
  • process.kill(pid, 0) throws ESRCH when the process does not exist and EPERM when it exists but you may not signal it.
  • Cluster and child processes each have their own PID; log it to see which worker handled a request.
  • Containers. PID 1 is special on Linux: it ignores default signal actions, so run a proper init or handle SIGTERM explicitly.
  • Parent watching. If your tool must stop when its parent dies, compare process.ppid over time or use an IPC channel and the disconnect event instead of polling.
  • Windows. Both properties exist, but the ppid of a process whose parent has ended may still show the old number.