从命令行停止 node.js 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10522532/
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:40:22  来源:igfitidea点击:

Stop node.js program from command line

node.jscommand

提问by Eleeist

I have a simple TCP server that listens on a port.

我有一个侦听端口的简单 TCP 服务器。

var net = require("net");

var server = net.createServer(function(socket) {
    socket.end("Hello!\n");
});

server.listen(7777);

I start it with node server.jsand then close it with Ctrl + Z on Mac. When I try to run it again with node server.jsI get this error message:

node server.js在 Mac 上使用 Ctrl + Z启动它然后关闭它。当我尝试再次运行它时,node server.js我收到此错误消息:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
          ^
Error: listen EADDRINUSE
at errnoException (net.js:670:11)
at Array.0 (net.js:771:26)
at EventEmitter._tickCallback (node.js:192:41)

Am I closing the program the wrong way? How can I prevent this from happening?

我是否以错误的方式关闭程序?我怎样才能防止这种情况发生?

回答by Brad

To end the program, you should be using Ctrl+ C. If you do that, it sends SIGINT, which allows the program to end gracefully, unbinding from any ports it is listening on.

要结束程序,您应该使用Ctrl+ C。如果你这样做,它会发送SIGINT,这允许程序优雅地结束,从它正在侦听的任何端口解除绑定。

See also: https://superuser.com/a/262948/48624

另见:https: //superuser.com/a/262948/48624

回答by Jamund Ferguson

Ctrl+Zsuspends it, which means it can still be running.

Ctrl+Z挂起它,这意味着它仍然可以运行。

Ctrl+Cwill actually kill it.

Ctrl+C实际上会杀死它。

you can also kill it manually like this:

你也可以像这样手动杀死它:

ps aux | grep node

Find the process ID (second from the left):

找到进程 ID(左起第二个):

kill -9 PROCESS_ID

This may also work

这也可能有效

killall node

回答by Hamid Tavakoli

Or alternatively you can do all of these in one line:

或者,您可以在一行中完成所有这些:

kill -9 $(ps aux | grep '\snode\s' | awk '{print }')

You can replace node inside '\snode\s' with any other process name.

您可以用任何其他进程名称替换 '\snode\s' 中的节点。

回答by Metagrapher

Resume and kill the process:

恢复并终止进程:

Ctrl+Zsuspends it, which means it is still running as a suspended background process.

Ctrl+Z挂起它,这意味着它仍然作为挂起的后台进程运行。

You are likely now at a terminal prompt...

您现在可能处于终端提示...

  1. Give the command fgto resume the process in the foreground.

  2. type Ctrl+Cto properly kill it.

  1. fg发出命令以在前台恢复进程。

  2. 键入Ctrl+C以正确杀死它。



Alternatively,you can kill it manually like this:

或者,您可以像这样手动杀死它:

(NOTE:the following commands may require root, so sudo ...is your friend)

注意:以下命令可能需要 root,sudo ...你的朋友也是如此)

pkill -9 node

or, if you don't have pkill, this may work:

或者,如果您没有 pkill,这可能会起作用:

killall node

or perhaps this:

或者这个:

kill $(ps -e | grep node | awk '{print }')

sometimes the process will list its own grep, in which case you'll need:

有时这个过程会列出它自己的 grep,在这种情况下你需要:

kill $(ps -e | grep dmn | awk '{print }')

.

.



h/t @ruffin from the comments on the question itself. I had the same issue and his comment helped me solve it myself.

h/t @ruffin 来自对问题本身的评论。我遇到了同样的问题,他的评论帮助我自己解决了这个问题。

回答by sunny1304

you can type .exitto quit node js REPL

你可以输入.exit退出节点js REPL

回答by sr77in

If you are running Node.js interactively (the REPL):

如果您以交互方式运行 Node.js(REPL):

Ctrl+ Cwill take back you to > prompt then type:

Ctrl+C将带您回到 > prompt 然后输入:

process.exit()

or just use Ctrl+ D.

或者只是使用Ctrl+ D

回答by Maxim Yefremov

$ sudo killall nodein another terminal works on mac, while killall nodenot working:

$ sudo killall node在另一个终端在mac 上killall node工作,但不工作:

$ killall node
No matching processes belonging to you were found

回答by Jason

on linux try: pkill node

在 linux 上尝试: pkill node

on windows:

在窗户上:

Taskkill /IM node.exe /F

or

或者

from subprocess import call

call(['taskkill', '/IM', 'node.exe', '/F'])

回答by randominstanceOfLivingThing

Though this is a late answer, I found this from NodeJS docs:

虽然这是一个迟到的答案,但我从NodeJS 文档中找到了这个:

The 'exit' event is emitted when the REPL is exited either by receiving the .exitcommand as input, the user pressing <ctrl>-Ctwice to signal SIGINT, or by pressing <ctrl>-Dto signal 'end' on the input stream. The listener callback is invoked without any arguments.

'exit' 事件在 REPL 通过接收.exit命令作为输入、用户按<ctrl>-C两次以发出 SIGINT 信号或通过按<ctrl>-D以发出输入流上的“结束”信号退出 REPL 时发出。不带任何参数调用侦听器回调。

So to summarize you can exit by:

总而言之,您可以通过以下方式退出:

  1. Typing .exitin nodejs REPL.
  2. Pressing <ctrl>-Ctwice.
  3. pressing <ctrl>-D.
  4. process.exit(0)meaning a natural exit from REPL. If you want to return any other status you can return a non zero number.
  5. process.kill(process.pid)is the way to kill using nodejs api from within your code or from REPL.
  1. 输入.exitnodejs REPL。
  2. <ctrl>-C两次。
  3. <ctrl>-D
  4. process.exit(0)意味着从 REPL 自然退出。如果您想返回任何其他状态,您可以返回一个非零数字。
  5. process.kill(process.pid)是从代码中或从 REPL 使用 nodejs api 杀死的方法。

回答by Decoded

I'm adding this answer because for many projects with production deployments, we have scripts that stop these processes so we don't have to.

我添加这个答案是因为对于许多具有生产部署的项目,我们有停止这些过程的脚本,所以我们不必。

A clean way to manage your Node Server processes is using the foreverpackage (from NPM).

管理节点服务器进程的一种干净方法是使用forever包(来自NPM)。

Example:

例子:

Install Forever

永远安装

npm install forever -g

npm install forever -g

Run Node Server

运行节点服务器

forever start -al ./logs/forever.log -ao ./logs/out.log -ae ./logs/err.log server.js

forever start -al ./logs/forever.log -ao ./logs/out.log -ae ./logs/err.log server.js

Result:

结果:

info: Forever processing file: server.js

info: Forever processing file: server.js

Shutdown Node Server

关闭节点服务器

forever stop server.js

forever stop server.js

Result

结果

info: Forever stopped process: uid command script forever pid id logfile uptime [0] sBSj "/usr/bin/nodejs/node" ~/path/to/your/project/server.js 23084 13176 ~/.forever/forever.log 0:0:0:0.247

info: Forever stopped process: uid command script forever pid id logfile uptime [0] sBSj "/usr/bin/nodejs/node" ~/path/to/your/project/server.js 23084 13176 ~/.forever/forever.log 0:0:0:0.247

This will cleanly shutdown your Server application.

这将彻底关闭您的服务器应用程序。