25

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?

MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
  • Possible duplicate of [stop all instances of node.js server](http://stackoverflow.com/questions/14790910/stop-all-instances-of-node-js-server) – rofrol Nov 28 '15 at 15:49

5 Answers5

47

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
rofrol
  • 14,438
  • 7
  • 79
  • 77
almypal
  • 6,612
  • 4
  • 25
  • 25
  • 4
    Great answer but... and for Windows? – MaiaVictor Jun 30 '12 at 18:07
  • Well just noticed not exactly what I was asking for but the commands can be used from node.js and if you change them a little (from process id to proccess name) it does what I asked, so this is the best answer I got. – MaiaVictor Jul 04 '12 at 01:40
  • Thanks! This is the only command that is working on cloud9 IDE to kill node.js process – Jone Polvora Feb 21 '13 at 20:46
  • 2
    for windows its much simpler that this, simply open task manager and sort by name and `end task` node.js process – Exlord Jun 12 '19 at 04:44
  • taskkill in windows requires admin rights to run. How can we do it within the npm script?? – Shishir Jul 14 '21 at 21:00
3

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])
Alex W
  • 37,233
  • 13
  • 109
  • 109
  • But how do I know the pid of the other running node.exe? – MaiaVictor Jun 30 '12 at 18:28
  • @Dokkat `console.log('This process is pid ' + process.pid);` will print the pid from the other Node.exe, then you execute the new one with a command line argument of that process's pid, and in the new script you send `SIGTERM` to the pid you have in `argv`. All of the information is available in that link above. – Alex W Jun 30 '12 at 18:35
  • 1
    @Viclib if you are still interested, I actually ran into this specific problem, and using `process.kill(pid, [signal])` was all I needed. I can give more info if you'd like. – daniel.caspers Mar 22 '16 at 20:41
  • @BlazeBiker fortunately, I actually managed to kill the process during those 4 years. Nether less, I'm sure some future readers would be glad to learn your findings as an answer to this question! – MaiaVictor Mar 22 '16 at 20:44
  • Just to add additional detail, I also posted my findings below in the answer: http://stackoverflow.com/a/36179604/2831961 – daniel.caspers Mar 23 '16 at 13:31
3

If you want to kill all processes than:

sudo killall -9 node

If you want to kill process on selected port than:

sudo kill sudo lsof -t -i:3100 

That was port 3100

Alexander Shtang
  • 1,819
  • 4
  • 15
  • 24
Renish Gotecha
  • 2,232
  • 22
  • 21
0

If sublime you say is sublimeText plugin, I have the same issue, and send TCP server a message 'shutdown' from python code, then

app.js

 TCPserver
        .on('connection', function(socket)
        {
            socket.pipe(require('through')
                (function(data)
                { //----------------------------
                    if (data.toString() === 'shutdown')
                    {
                        process.exit();
                    }
                    //--------------------------
                }));
            socket.end();
        })
0

Similarly to what @Alex W said, you can send a kill signal to the process so long as you have its process ID, or PID using the following node function:

process.kill(pid, [signal])

In my case, I had the PIDs readily available as I was spawning child_process().spawn.pid. I have tested it and it does work on Win 7 x64.

daniel.caspers
  • 1,660
  • 1
  • 18
  • 22