How to kill an open process on node.js?

前端 未结 5 1598
滥情空心
滥情空心 2020-12-07 16:53

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

相关标签:
5条回答
  • 2020-12-07 17:33

    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 讨论(0)
  • 2020-12-07 17:38

    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
    
    0 讨论(0)
  • 2020-12-07 17:38

    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])
    
    0 讨论(0)
  • 2020-12-07 17:38

    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

    0 讨论(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.

    0 讨论(0)
提交回复
热议问题