Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop the node.js inspector / Chrome Debugger on SIGINT?

I have the following code to capture a ^C from the terminal and gracefully shutdown my Express app:

process.on('SIGINT', () => {
    console.log('SIGINT received ...');
    console.log('Shutting down the server');

    server.close(() => {
        console.log('Server has been shutdown');
        console.log('Exiting process ...');
        process.exit(0);
    });
});

However if I start my node instance with --inspect, then the above code fails to stop the inspector and the Chrome debugger. When I restart my application, I get the following error:

Starting inspector on 127.0.0.1:9229 failed: address already in use

How do I gracefully stop my app to avoid this error?

Full code available here.

like image 224
Naresh Avatar asked Dec 02 '17 15:12

Naresh


4 Answers

It seems that VSCode, Puppeteer, nodemon, express, etc. causes this problem, you ran a process in the background or just closed the debugging area [browser, terminal, etc. ] or whatever , anyway, you can in CMD run

$ ps ax | grep node

then

$ killall -9 node

it is not the best solution, also, i may suggest that you look for the process using this port then send close signal

$ sudo ss -lptn 'sport = :9229'

OR

$ sudo netstat -nlp | grep :9229

OR

$ sudo lsof -n -i :9229 | grep LISTEN

THEN:

$ sudo kill <pid>

OR JUST [the two steps in one]

$ sudo kill `sudo lsof -t -i:9229`

OR

$ sudo kill $(sudo lsof -t -i:9229)
like image 110
KhogaEslam Avatar answered Oct 30 '22 12:10

KhogaEslam


This works for me:

fuser -k -n tcp 9229
like image 41
Gi5 Avatar answered Oct 30 '22 13:10

Gi5


I had the same terminal error from nodemon, even though I thought I'd quit all terminal processes, but simply quitting VSCode and reopening solved for me (thanks to KhogaEslam's answer for the hint).

Hope this helps someone else too!

like image 20
Joel Balmer Avatar answered Oct 30 '22 12:10

Joel Balmer


To kill all node process

taskkill /im node.exe

or forcefully

taskkill /f /im node.exe

like image 29
Nikhil Vats Avatar answered Oct 30 '22 11:10

Nikhil Vats