process.pid

Returns the operating system process ID of the current Node.js process.

Signature
process.pid
  • Added in Node.js v0.1.15

Return value

integerThe PID assigned to this process by the operating system.

process.pid is the operating system's identifier for this process. It is fixed for the lifetime of the process and shared by every thread inside it, including Worker threads.

When to reach for it

  • Logging. When several workers write to the same log stream or the same file, the PID is what lets you follow one process's story through the interleaving.
  • Signals. It is the number you give kill, or that a supervisor uses to send SIGTERM during a graceful shutdown.
  • Correlation. Matching a Node process against top, ps or a container's metrics view.

What trips people up

  • PIDs are reused. After a process exits the OS is free to hand the number to something unrelated, so a stale PID file can point at a completely different program. Never send a signal based on a PID file without verifying the process is really yours.
  • Workers share it. Worker threads report the same process.pid as the main thread; use threadId from node:worker_threads to tell threads apart. Cluster workers are separate processes and do have distinct PIDs.
  • Containers renumber. Inside a PID namespace your process is often PID 1, which also means it receives no default signal handling — an unhandled SIGTERM will not stop it.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Tag every log line so output from several workers can be untangled.
  • Write a PID file that a service manager or a deploy script can signal.
  • Correlate a Node process with its row in ps, top or a container dashboard.
  • Distinguish cluster workers in metrics without adding your own identifier.

Frequently asked questions

Does process.pid change while the program runs?

No — the PID is assigned when the process starts and stays the same until it exits, and every thread in the process reports the same value. A restarted process gets a new PID, so never persist one as a long-lived identifier.

How do I get the parent process ID in Node?

Read process.ppid, added in v9.2.0, which returns the PID of the process that started this one. Inside a container that is often 0 or 1 because the real parent lives outside the PID namespace.

Why do my Worker threads all report the same process.pid?

Because worker_threads run inside a single OS process and PIDs are per process, not per thread. Use `threadId` from node:worker_threads to identify a thread, or the cluster module if you need genuinely separate processes.

More examples

A PID file that survives PID reuse

The output shows a crashed previous run being cleaned up rather than blocking the new one. `process.kill(pid, 0)` sends no signal — it only asks whether the PID exists, which is the check that makes a PID file usable at all. It still cannot prove the process is *your* server, since the OS may have recycled 4711, so production supervisors pair the PID with a start time or a lock file.

Example · javascript
const fs = require('node:fs');

const PID_FILE = 'server.pid';

function isRunning(pid) {
  try {
    process.kill(pid, 0); // signal 0 only tests for existence
    return true;
  } catch (err) {
    return err.code === 'EPERM'; // exists, but owned by another user
  }
}

if (fs.existsSync(PID_FILE)) {
  const existing = Number(fs.readFileSync(PID_FILE, 'utf8').trim());
  if (isRunning(existing)) {
    console.error(`Already running as pid ${existing}`);
    process.exit(1);
  }
  console.log(`Removing stale pid file for ${existing}`);
}

fs.writeFileSync(PID_FILE, String(process.pid));
process.on('exit', () => fs.rmSync(PID_FILE, { force: true }));

console.log(`started as pid ${process.pid}`);
// Removing stale pid file for 4711
// started as pid 5230

Distinguishing cluster workers from Worker threads

The output shows the split clearly: each cluster worker is a separate OS process with its own PID, while the worker threads inside them share their parent's PID and are only distinguishable by threadId. That is why a log prefix built from the PID alone merges thread output together — pair `process.pid` with `threadId` when a process runs threads.

Example · javascript
const cluster = require('node:cluster');
const { Worker, isMainThread, threadId } = require('node:worker_threads');

if (cluster.isPrimary) {
  console.log(`primary pid ${process.pid}`);
  cluster.fork();
  cluster.fork();
} else if (isMainThread) {
  console.log(`cluster worker ${cluster.worker.id}: pid ${process.pid}, thread ${threadId}`);
  new Worker(__filename);
} else {
  console.log(`  worker thread: pid ${process.pid}, thread ${threadId}`);
}
// primary pid 5230
// cluster worker 1: pid 5231, thread 0
// cluster worker 2: pid 5232, thread 0
//   worker thread: pid 5231, thread 1
//   worker thread: pid 5232, thread 1

Discussion

  • Be the first to comment on this lesson.