简体   繁体   English

如何杀死 node.js 上的打开进程?

[英]How to kill an open process on node.js?

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.我正在尝试在 sublime 上为 Node.js 设置一个构建系统,因此我可以按 F7 在打开的文件上调用“node”。 The problem is that the process is then open forever, so, the second time I use F7 I get an add-in-use.问题是该过程将永远打开,因此,第二次使用 F7 时,我得到了一个加载项。

Is there a way I can kill the openned "node.exe" process from node.js?有没有办法可以从 node.js 中杀死打开的“node.exe”进程?

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在 Windows 上,您可以使用以下步骤

  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.请注意与您的节点进程对应的 PID,在本例中为 3592。接下来运行 taskkill 以终止该进程。

  C:\> taskkill /F /PID 3592

Or /IM switch或 /IM 开关

  C:\> taskkill /F /IM node.exe

From within Node.js:在 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 (ie not Windows) that will exit a process by its process id.有某些用于退出的方法仅适用于 POSIX(即不是 Windows),它们将通过进程 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:另请注意,您可能能够使用此方法发送 kill() 信号,但这并不表示它不适用于 Windows:

process.kill(pid, [signal])

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那是端口3100

If sublime you say is sublimeText plugin, I have the same issue, and send TCP server a message 'shutdown' from python code, then如果你说的 sublime 是 sublimeText 插件,我有同样的问题,并从 python 代码向 TCP 服务器发送消息“关闭”,然后

app.js应用程序.js

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

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:与@Alex W 所说的类似,只要您拥有进程 ID 或使用以下节点函数的 PID,您就可以向该进程发送终止信号:

process.kill(pid, [signal])

In my case, I had the PIDs readily available as I was spawning child_process().spawn.pid .就我而言,当我child_process().spawn.pid ,我可以随时使用 PID。 I have tested it and it does work on Win 7 x64.我已经对其进行了测试,它确实可以在 Win 7 x64 上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM