Course contentsShow
← JavaScript
Lesson 1501 of 2,72333. JavaScript in Node.jsFree lesson

Process.pid and Process.ppid

Identifying the current process ID and parent process ID for debugging and logging.

Process.pid and Process.ppid

You'll learn how to identify the current process and its parent process using built-in Node.js identifiers.


Every running Node.js program is a process — an instance of your application executing on the operating system. Each process gets a unique identifier called a process ID (PID). The process.pid property gives you this number for your current Node.js process.

Similarly, most processes are started by another process (the parent). You can access the parent's ID using process.ppid (parent process ID).

Think of it like family lineage: your process has an ID badge (process.pid), and you can also see your parent's ID badge (process.ppid). Your terminal or shell is often the parent that spawned your Node.js script.

Why This Matters

These identifiers are invaluable for:

  • Logging: Including the PID in log files helps you correlate logs when multiple instances run simultaneously
  • Debugging: Identifying which specific process instance is misbehaving
  • Process management: Tracking or killing processes using system tools
  • Inter-process communication: Processes can signal each other by PID

Basic Usage

console.log('Current process ID:', process.pid);
console.log('Parent process ID:', process.ppid);

// Example output:
// Current process ID: 12345
// Parent process ID: 9876

Each time you run your script, process.pid will be different (the OS assigns a new ID), but process.ppid will typically be your terminal's process ID.

Key Takeaway: Use process.pid and process.ppid to identify your Node.js process and its parent, essential for logging, debugging, and process management in server applications.