Can't find out where does a node.js app running and can't kill it

Linuxnode.js

Linux Problem Overview


What I did: I have just set up node environment, installed express, create and installed an express project

express hello
cd hello && npm install

then started the app with "node app".

Environment:

yole@Yole:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 7.2 (wheezy)
Release:	7.2
Codename:	wheezy

yole@Yole:~$ node --version
v0.10.22

yole@Yole:~$ express --version
3.4.4

Problem When I want to stop this app, I used CTRL+C, but the thing I found is it did not stopped. Then I restarted the server! I found I can still access the page in browser. Orz.

I have tried the following thing but still can't find out the running process.

yole@Yole:~$ killall node
node: no process found

yole@Yole:~$ ps -ef|grep node
yole      3161  2888  0 16:57 pts/1    00:00:00 grep node

yole@Yole:~$ netstat -apn|grep 3000

Question How to find out the running node process or how to kill it.

===== update It is very strange that all browses in my machine can visit the site while it's not available on other machine! I only visit the page with Chrome before I stop the application. It seems to be a cache problem, but how cache shared among browsers..

Linux Solutions


Solution 1 - Linux

List node process:

$ ps -e|grep node

Kill the process using

$kill -9 XXXX

Here XXXX is the process number

Solution 2 - Linux

If you want know, the how may nodejs processes running then you can use this command

ps -aef | grep node

So it will give list of nodejs process with it's project name. It will be helpful when you are running multipe nodejs application & you want kill specific process for the specific project.

Above command will give output like

XXX  12886  1741  1 12:36 ?        00:00:05 /home/username/.nvm/versions/node/v9.2.0/bin/node --inspect-brk=43443 /node application running path.

So to kill you can use following command

kill -9 12886

So it will kill the spcefic node process

Solution 3 - Linux

You can kill all node processes using pkill node

or you can do a ps T to see all processes on this terminal
then you can kill a specific process ID doing a kill [processID] example: kill 24491

Additionally, you can do a ps -help to see all the available options

Solution 4 - Linux

I use fkill

INSTALL

npm i fkill-cli -g

EXAMPLES

Search process in command line

fkill

OR: kill ! ALL process

fkill node

OR: kill process using port 8080

fkill :8080

Solution 5 - Linux

Coming to this from a macOS - investigating with these 3 commands are often helpful:

replace 6006 with a meaningful port of your apps URL, in my case I was hunting for localhost:6006 which was not a running app but still available when hitting the URL in browser.

Netstat

netstat -anp tcp | grep 6006

This will report the type of connection and command that is running it, from here you can investigate the TCP states:

tcp4       0      0  127.0.0.1.58473        127.0.0.1.6006         CLOSE_WAIT
tcp4       0      0  127.0.0.1.6006         127.0.0.1.58471        FIN_WAIT_2
tcp4       0      0  127.0.0.1.58471        127.0.0.1.6006         CLOSE_WAIT
tcp4       0      0  127.0.0.1.6006         *.*                    LISTEN
tcp4       0      0  127.0.0.1.58468        127.0.0.1.6006         TIME_WAIT

lsof

lsof -i tcp:6006

This will list the command name and PID.

Optionally after this, you can use kill along with the PID number to stop the processes from running. ex: kill #####

COMMAND     PID       USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node       8193 username   26u  IPv4 0x8793aae78e568e01      0t0  TCP localhost:6006 (LISTEN)
Google    93430 username   67u  IPv4 0x8793aae79530c941      0t0  TCP localhost:58471->localhost:6006 (CLOSE_WAIT)
Google    93430 username   68u  IPv4 0x8793aae795d98ba1      0t0  TCP localhost:58473->localhost:6006 (CLOSE_WAIT)

Finally to find where the node process is running you can use ps and here, instead of using node as others have mentioned use the port number itself that you are looking for:

ps

ps -aef | grep 6006

This will report back something like this:

  501  8193  8179   0 24May21 ttys008    1:08.98 /Users/username/.nvm/versions/node/v12.10.0/bin/node /Users/username/Documents/repositories/react-application/node_modules/.bin/start-storybook -h localhost -p 6006
  501 77757  9725   0 12:50PM ttys013    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox 6006

Now we can confirm the PID from our lsof command matches the PID printed by the ps, we can see when the process was started and we are seeing the path to our persistent app.

Lastly we can use kill #### to stop the process.

Solution 6 - Linux

If all those kill process commands don't work for you, my suggestion is to check if you were using any other packages to run your node process.

I had the similar issue, and it was due to I was running my node process using PM2(a NPM package). The kill [processID] command disables the process but keeps the port occupied. Hence I had to go into PM2 and dump all node process to free up the port again.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionyuyue007View Question on Stackoverflow
Solution 1 - LinuxDamodaranView Answer on Stackoverflow
Solution 2 - LinuxBhaurao BirajdarView Answer on Stackoverflow
Solution 3 - LinuxConfusedDeerView Answer on Stackoverflow
Solution 4 - LinuxJohan HoeksmaView Answer on Stackoverflow
Solution 5 - LinuxIan RobertsView Answer on Stackoverflow
Solution 6 - LinuxGinView Answer on Stackoverflow