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.
console.log(process.pid, process.ppid);
Try it
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.
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
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.pidat startup and delete the file on exit, and check withprocess.kill(pid, 0)whether an older instance is still alive. process.kill(pid, 0)throwsESRCHwhen the process does not exist andEPERMwhen 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
SIGTERMexplicitly. - Parent watching. If your tool must stop when its parent dies, compare
process.ppidover time or use an IPC channel and thedisconnectevent instead of polling. - Windows. Both properties exist, but the
ppidof a process whose parent has ended may still show the old number.