KoderSolution Logo
HomeArticlesTutorialsForumAI LabRun Code
KoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Engineering Newsletter

Join 100,000+ engineers receiving curated high-signal content weekly.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
🟢

Node.js

Topic Hub & Articles

Node.js Intro

10 min

Node.js Get Started

10 min

Node.js Modules

10 min

Node.js HTTP Module

10 min

Node.js File System

10 min

Node.js URL Module

10 min

Node.js NPM

10 min

Node.js Events

10 min

Node.js Upload Files

10 min

Node.js Email

10 min

Node.js Buffer

10 min

Recap Quiz

5 Questions

Node.js Streams

10 min

Node.js Crypto

10 min

Node.js OS Module

10 min

Node.js Path Module

10 min

Node.js Global Objects

10 min

Recap Quiz

5 Questions

Node.js Process

10 min

Node.js Child Processes

10 min

Node.js Worker Threads

10 min

Node.js DNS Module

10 min

Node.js Query String

10 min

Recap Quiz

5 Questions

MySQL Connect

10 min

MySQL Create Database

10 min

Recap Quiz

5 Questions

MySQL Order By

10 min

Recap Quiz

5 Questions

MongoDB Intro

10 min

MongoDB Create Database

10 min

MongoDB Create Collection

10 min

MongoDB Insert

10 min

Recap Quiz

5 Questions

MongoDB Find

10 min

MongoDB Query

10 min

MongoDB Sort

10 min

MongoDB Delete

10 min

MongoDB Update

10 min

Recap Quiz

5 Questions

MongoDB Limit

10 min

MongoDB Join

10 min

Progress
0%

0 / 35 Lessons

Node.jsNode.js Tutorial
Lesson

Node.js Process

10 min reading
Free Course

Node.js Process: Environment Variables, Signals & Runtime Control

The process global object provides information about, and control over, the current Node.js runtime process instance. It allows reading environment variables, terminating processes, receiving OS signals, and reading standard I/O streams.

Process Lifecycle & OS Signals

flowchart LR
    A["OS / Docker / K8s Manager"] -->|"SIGTERM / SIGINT signal"| B["process.on('SIGTERM') Listener"]
    B --> C["Graceful Shutdown: Close DB & Server"]
    C --> D["process.exit(0)"]

Practical Code Example

import http from 'node:http';

// 1. Inspect Environment Variables & Arguments
const PORT = process.env.PORT || 5000;
const NODE_ENV = process.env.NODE_ENV || 'development';

console.log(`Starting process in [${NODE_ENV}] mode on PID ${process.pid}`);
console.log('CLI Arguments:', process.argv.slice(2));

// 2. Graceful Shutdown HTTP Server Setup
const server = http.createServer((req, res) => {
    res.writeHead(200);
    res.end('Server Active');
});

server.listen(PORT, () => console.log(`Server live on port ${PORT}`));

// Graceful Termination Signal Listener
function handleShutdown(signal) {
    console.log(`
Received ${signal}. Initiating graceful shutdown...`);

    server.close(() => {
        console.log('HTTP Server closed. Database connections released.');
        process.exit(0); // Exit code 0 indicates clean exit
    });

    // Force terminate if graceful cleanup hangs
    setTimeout(() => {
        console.error('Forced shutdown due to timeout.');
        process.exit(1);
    }, 10000).unref();
}

process.on('SIGINT', () => handleShutdown('SIGINT'));   // Ctrl+C
process.on('SIGTERM', () => handleShutdown('SIGTERM')); // Container Stop Signal

Best Practices & Gotchas

  • Use Exit Code 0 for Success, 1 for Failures: Exit with process.exit(0) on clean shutdowns and non-zero (e.g. process.exit(1)) on unrecoverable errors.
  • Implement Graceful Shutdowns: Always listen for SIGTERM and SIGINT signals to close active database pools and complete pending HTTP requests before exiting.
  • Avoid Storing Secrets Directly in .env Files in Production: Pass environment variables directly via host runtime containers or secret managers.

Self-Check Challenge

Write code to listen for process.on('uncaughtException') and print the error message before exiting gracefully.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Recommended Courses & Books

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum