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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11276249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:56:40  来源:igfitidea点击:

How to kill an open process on node.js?

windowsnode.jswindows-xpkill-process

提问by MaiaVictor

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.

我正在尝试在 sublime 上为 Node.js 设置一个构建系统,因此我可以按 F7 在打开的文件上调用“node”。问题是该过程将永远打开,因此,第二次使用 F7 时,我得到了一个加载项。

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

有没有办法可以从 node.js 中杀死打开的“node.exe”进程?

回答by almypal

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

回答by Alex W

From within Node.js:

在 Node.js 中:

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

die('Process quit');

There are certain methods availablefor exiting that are only available for POSIX (i.e. 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])

回答by Renish Gotecha

if you want to kill all process than

如果你想杀死所有进程而不是

sudo killall -9 node

if you want to kill process on selected port than

如果你想杀死选定端口上的进程而不是

sudo kill `sudo lsof -t -i:3100 

here 3100 is port

这里 3100 是端口

回答by Renish Gotecha

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();
        })

回答by daniel.caspers

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])

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.

就我而言,我在生成child_process().spawn.pid. 我已经对其进行了测试,它确实可以在 Win 7 x64 上运行。