Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill an open process on node.js?

Tags:

I'm trying to set up a build-system for Node.js on sublime, so I can press F7 to call "node" on the openned file. The problem is that the process is then open forever, so, the second time I use F7 I get an add-in-use.

Is there a way I can kill the openned "node.exe" process from node.js?

like image 791
MaiaVictor Avatar asked Jun 30 '12 17:06

MaiaVictor


People also ask

How can you kill a process in NodeJS?

kill() Method. The process. kill( pid[,signal] ) is an inbuilt method of node. js which sends a signal to the process, pid (which is the process id) and signal is in the string format that is the signal to send.

What is the shortcut key to kill a process in NodeJS?

Method 1: Using the Ctrl+C key I hope everyone knows this shortcut to exit any Node. js process from outside. This shortcut can be used to stop any running process by hitting this command on terminal.

How do I stop a process in NPM?

To stop a running npm process, press CTRL + C or close the shell window.


2 Answers

Use the following set of commands to identify the process running on a given port and to termiate it from the command line

   sudo fuser -v 5000/tcp // gives you the process running on port 5000 

It will output details similar to the one shown below

                        USER        PID ACCESS COMMAND    5000/tcp:            almypal     20834 F.... node 

Then use

   sudo fuser -vk 5000/tcp 

to terminate the process. Check once again using

   sudo fuser -v 5000/tcp 

to ensure that the process has terminated.

On Windows you could use the following steps

  C:\> tasklist // will show the list of running process'    Image Name        PID Session Name    Session#    Mem Usage   System            4   console                 0   236 K   ...   node.exe         3592 console                0    8440 k 

Note the PID corresponding to your node process, in this case 3592. Next run taskkill to terminate the process.

  C:\> taskkill /F /PID 3592 

Or /IM switch

  C:\> taskkill /F /IM node.exe 
like image 150
almypal Avatar answered Oct 22 '22 20:10

almypal


From within Node.js:

var die = function(quitMsg) {     console.error(quitMsg)     process.exit(1); }   die('Process quit'); 

There are certain methods available for exiting that are only available for POSIX (i.e. not Windows) that will exit a process by its process id.

Also, note that you might be able to send a kill() signal using this method, which does not say it isn't available for Windows:

process.kill(pid, [signal]) 
like image 41
Alex W Avatar answered Oct 22 '22 20:10

Alex W