How to Properly Stop a Node.js Server Running in PuTTY Terminal


2 views

When you run a Node.js server in PuTTY with the command node server.js, it creates a foreground process that continues running until terminated. The Pause/Break key doesn't work because it's not designed for process control in Linux/Unix environments.

Here are the proper ways to stop your Node.js server:

// Method 1: CTRL+C (recommended)
// Simply press CTRL+C in the terminal where the server is running
// This sends SIGINT (interrupt signal) to the process

// Method 2: Using process ID
// First find the process ID:
ps aux | grep node

// Then kill it:
kill -9 [process_id]

For production servers, it's better to implement proper shutdown handling:

// server.js
const server = require('http').createServer();

server.listen(3000, () => {
  console.log('Server running on port 3000');
});

process.on('SIGTERM', () => {
  console.log('SIGTERM received. Shutting down gracefully');
  server.close(() => {
    console.log('Server closed');
    process.exit(0);
  });
});

process.on('SIGINT', () => {
  console.log('SIGINT (Ctrl+C) received. Shutting down');
  server.close(() => {
    console.log('Server closed');
    process.exit(0);
  });
});

Other approaches worth considering:

  • Using forever or pm2 process managers
  • Creating custom shutdown endpoints
  • Implementing health checks for automated management

When working with PuTTY:

  1. The terminal session must remain active for CTRL+C to work
  2. Closing PuTTY window doesn't automatically terminate Node processes
  3. For detached processes, use nohup or screen

When you execute node server.js in a PuTTY terminal, you're creating a foreground process. Unlike GUI applications, command-line processes in Linux/Unix environments require specific termination methods.

The standard way to stop a running Node.js process is by sending an interrupt signal:

// While your server is running in terminal
Press Ctrl+C

This sends SIGINT (signal interrupt) to your Node.js application. For well-designed servers, this should trigger graceful shutdown procedures.

When Ctrl+C doesn't work (which sometimes happens with network servers), try these approaches:

Using process.kill()

From another terminal session:

// Find the process ID first
ps aux | grep node

// Then kill it explicitly
kill -9 [PID]

Graceful Shutdown in Code

Implement proper signal handling in your server.js:

const server = require('http').createServer((req, res) => {
  res.end('Hello World');
});

server.listen(3000);

process.on('SIGTERM', () => {
  server.close(() => {
    console.log('Server gracefully terminated');
    process.exit(0);
  });
});

process.on('SIGINT', () => {
  server.close(() => {
    console.log('Server terminated by user');
    process.exit(0);
  });
});

For production deployments, consider using process managers that handle lifecycle events:

// PM2 example
pm2 start server.js
pm2 stop server

If your Node.js server becomes unresponsive:

// List all node processes
pgrep -l node

// Force kill all node processes (use with caution)
pkill -9 node