Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quitting node.js gracefully

Tags:

node.js

I'm reading through the excellent online book http://nodebeginner.org/ and trying out the simple code

var http = require("http");  function onRequest(request, response) {   response.writeHead(200, {"Content-Type": "text/plain"});   response.write("Hello World");   response.end(); }  http.createServer(onRequest).listen(8888);  

Now I didn't know (and I still don't know!) how to shut down node.js gracefully, so I just went ctrl+z. Now each time I try to run node server.js I get the following error messages.

node.js:134         throw e; // process.nextTick error, or 'error' event on first tick         ^ Error: EADDRINUSE, Address already in use     at Server._doListen (net.js:1100:5)     at net.js:1071:14     at Object.lookup (dns.js:153:45)     at Server.listen (net.js:1065:20)     at Object.<anonymous> (/Users/Bob/server.js:7:4)     at Module._compile (module.js:402:26)     at Object..js (module.js:408:10)     at Module.load (module.js:334:31)     at Function._load (module.js:293:12)     at Array.<anonymous> (module.js:421:10) 

So, two questions:

1) How do I shut down node.js gracefully?

2) How do I repair the mess I've created?

like image 904
Randomblue Avatar asked Aug 05 '11 15:08

Randomblue


People also ask

Is node js still relevant 2022?

Node. js development has become very popular over the last four years and continues to stand the competition in 2022 making startups worldwide choose it over other available options.

Is node js still relevant in 2021?

The short answer is “NO.” The long answer is, “NO, it's not dead, and it probably will never die.” Node. js is just as relevant to coding in 2021 and beyond, even if the hype around it has stabilized slightly.

Why was graceful shut down?

A graceful shutdown is when a computer is turned off by software function and the operating system (OS) is allowed to perform its tasks of safely shutting down processes and closing connections.


2 Answers

I currently use Node's event system to respond to signals. Here's how I use the Ctrl-C (SIGINT) signal in a program:

process.on( 'SIGINT', function() {   console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );   // some other closing procedures go here   process.exit( ); }) 

You were getting the 'Address in Use' error because Ctrl-Z doesn't kill the program; it just suspends the process on a unix-like operating system and the node program you placed in the background was still bound to that port.

On Unix-like systems, [Control+Z] is the most common default keyboard mapping for the key sequence that suspends a process (SIGTSTP).[3] When entered by a user at their computer terminal, the currently running foreground process is sent a SIGTSTP signal, which generally causes the process to suspend its execution. The user can later continue the process execution by typing the command 'fg' (short for foreground) or by typing 'bg' (short for background) and furthermore typing the command 'disown' to separate the background process from the terminal.1

You would need to kill your processes by doing a kill <pid> or 'killall -9 node' or the like.

like image 69
EhevuTov Avatar answered Sep 23 '22 12:09

EhevuTov


  1. Use Ctrl+C to exit the node process gracefully

  2. To clean up the mess depends on your platform, but basically you need to find the remains of the process in which node was running and kill it.

    For example, on Unix: ps -ax | grep node will give you an entry like:

    1039 ttys000    0:00.11 node index.js 

    where index.js is the name of your node file.

    In this example, 1039 is the process id (yours will be different), so kill -9 1039 will end it, and you'll be able to bind to the port again.

like image 29
avstrallen Avatar answered Sep 23 '22 12:09

avstrallen