1

I am creating an application in Nodejs using Expressjs framework.

I was try to execute some script after every minute using setInterval() JavaScript method.

When I start my server by node app.js the script run successfully. But when I stop the server by Ctrl+c and trying to restart it then it says that the 3000 (my server port) already in use.

I cannot restart my server neither access my website. I am using Ubuntu.

I have used all the following commands but it doesn't stop.

  1. fuser -k 3000/tcp

  2. kill processNumber

  3. killall node

Code:

 router.get("/trackplayers", function(req, res, next ) {
    // Run in every minute
    setInterval(function(){
       console.log('Players tracking is under process..');
    }, 60000);
 });

Thanks in advance!

filype
  • 8,034
  • 10
  • 40
  • 66
Tahir
  • 483
  • 1
  • 4
  • 17
  • 1
    If you paste code will help.. but as per my understading instead of set interval better we go with settimout with call back.. Which using settimout you will be able to execute a function once execution completed in callback again create new settimeout. – BEJGAM SHIVA PRASAD Nov 15 '16 at 06:18
  • After pressing `Ctrl+c`, is the process still seems working when you run `ps aux`? – Bünyamin Sarıgül Nov 15 '16 at 06:37
  • no the process is not there when I type ps aux or ps aux | grep node – Tahir Nov 15 '16 at 07:08

1 Answers1

0

Be sure to exit the process cleanly. The following code will gracefully terminate an express application.

var express = require("express");
var app = express();
var server = app.listen(3000);
// Respond to 'Ctrl+C'
process.on("SIGINT", function () {
  // stop accepting connections
  server.close(function () {
    // connections are closed
    // exit the process
    process.exit(0);
  });
});
// Server is shutting down
process.on("SIGTERM", function () {
  // stop accepting connections
  server.close(function () {
    // connections are closed
    // exit the process
    process.exit(0);
  });
});
Dan Nagle
  • 4,384
  • 1
  • 16
  • 28