停止节点。来自命令行的js程序。

时间:2021-02-15 20:53:36

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.js and then close it with Ctrl + Z on Mac. When I try to run it again with node server.js I get this error message:

我从节点服务器开始。然后在Mac上用Ctrl + Z关闭它。当我尝试用节点服务器再次运行它时。我得到这个错误信息:

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?

我是不是把程序关错了?我怎样才能防止这种情况发生呢?

11 个解决方案

#1


301  

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

#2


256  

Ctrl+Z suspends it, which means it can still be running.

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

Ctrl+C will 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

#3


27  

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

或者你可以用一行来做所有这些:

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

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

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

#4


18  

you can type .exit to quit node js REPL

您可以键入.exit退出node js REPL。

#5


16  

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

如果您正在运行节点。js交互(REPL):

Ctrl + C will take back you to > prompt then type:

Ctrl + C会把你带回>提示然后输入:

process.exit()

or just use Ctrl + D.

或者用Ctrl + D。

#6


12  

$ sudo killall node in another terminal works on mac, while killall node not working:

$ sudo killall节点在mac上的另一个终端工作,而killall节点不工作:

$ killall node
No matching processes belonging to you were found

#7


10  

Resume and kill the process:

Ctrl+Z suspends 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 fg to resume the process in the foreground.

    让命令fg在前台恢复进程。

  2. type Ctrl+C to properly kill it.

    键入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 $1}')

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

有时流程会列出自己的grep,在这种情况下,您需要:

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

.


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来自对问题本身的评论。我也有同样的问题,他的评论帮助我自己解决了这个问题。

#8


4  

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 .exit command as input, the user pressing <ctrl>-C twice to signal SIGINT, or by pressing <ctrl>-D to signal 'end' on the input stream. The listener callback is invoked without any arguments.

当REPL通过接收.exit命令作为输入退出时,“退出”事件就会被释放,用户按下 -C两次以发送SIGINT信号,或者按下 -D以在输入流上显示“结束”。侦听器回调是在没有任何参数的情况下调用的。

So to summarize you can exit by:

总结一下,你可以退出:

  1. Typing .exit in nodejs REPL.
  2. 键入.exit in nodejs REPL。
  3. Pressing <ctrl>-C twice.
  4. 按< ctrl > - c两次。
  5. pressing <ctrl>-D.
  6. 按< ctrl > - d。
  7. process.exit(0) meaning a natural exit from REPL. If you want to return any other status you can return a non zero number.
  8. exit(0)表示从REPL中自然退出。如果您想返回任何其他状态,您可以返回一个非零数。
  9. process.kill(process.pid) is the way to kill using nodejs api from within your code or from REPL.
  10. kill(进程。pid)是在代码中或从REPL中使用nodejs api的方法。

#9


2  

If you want to stop your server with npm stop or something like this. You can write the code that kill your server process as:

如果您想要停止您的服务器与npm停止或类似的事情。您可以编写杀死服务器进程的代码:

require('child_process').exec(`kill -9 ${pid}`)

Check this link for the detail: https://gist.github.com/dominhhai/aa7f3314ad27e2c50fd5

查看此链接的详细信息:https://gist.github.com/dominhhai/aa7f3314ad27e2c50fd5。

#10


2  

I ran into an issue where I have multiple node servers running, and I want to just kill one of them and redeploy it from a script.

我遇到了一个问题,我有多个节点服务器在运行,我想要杀死其中一个,然后从脚本中重新部署它。

Note: This example is in a bash shell on Mac.

注意:这个例子在Mac上的bash shell中。

To do so I make sure to make my node call as specific as possible. For example rather than calling node server.js from the apps directory, I call node app_name_1/app/server.js

要做到这一点,我必须确保我的节点调用尽可能具体。例如,而不是调用节点服务器。从应用程序目录中,我调用了node app_name_1/app/server.js。

Then I can kill it using:

然后我可以用:

kill -9 $(ps aux | grep 'node\ app_name_1/app/server.js' | awk '{print $2}')

kill -9 $(ps aux | grep 'node\ app_name_1/app/服务器)。js' | awk '{print $2}'

This will only kill the node process running app_name_1/app/server.js.

这只会杀死运行app_name_1/app/server.js的节点进程。

If you ran node app_name_2/app/server.js this node process will continue to run.

如果运行节点app_name_2/app/服务器。这个节点进程将继续运行。

If you decide you want to kill them all you can use killall node as others have mentioned.

如果您决定要杀死它们,您可以像其他人提到的那样使用killall节点。

#11


2  

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 forever package (from NPM).

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

Example:

Install Forever

npm install forever -g

永远npm安装- g

Run Node Server

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

永远永远的开始过程。/日志/。日志ao。/日志/。日志ae。/日志/犯错。日志server.js

Result:

info: Forever processing file: server.js

信息:永久处理文件:server.js。

Shutdown Node Server

forever stop server.js

永远停止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

信息:永久停止进程:uid命令脚本永远的pid id日志文件uptime [0] sBSj "/usr/bin/nodejs/node" ~/path/to/你/项目/服务器。js 13176 ~ 23084 / .forever /永远。日志0:0:0:0.247

This will cleanly shutdown your Server application.

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

#1


301  

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

#2


256  

Ctrl+Z suspends it, which means it can still be running.

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

Ctrl+C will 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

#3


27  

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

或者你可以用一行来做所有这些:

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

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

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

#4


18  

you can type .exit to quit node js REPL

您可以键入.exit退出node js REPL。

#5


16  

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

如果您正在运行节点。js交互(REPL):

Ctrl + C will take back you to > prompt then type:

Ctrl + C会把你带回>提示然后输入:

process.exit()

or just use Ctrl + D.

或者用Ctrl + D。

#6


12  

$ sudo killall node in another terminal works on mac, while killall node not working:

$ sudo killall节点在mac上的另一个终端工作,而killall节点不工作:

$ killall node
No matching processes belonging to you were found

#7


10  

Resume and kill the process:

Ctrl+Z suspends 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 fg to resume the process in the foreground.

    让命令fg在前台恢复进程。

  2. type Ctrl+C to properly kill it.

    键入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 $1}')

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

有时流程会列出自己的grep,在这种情况下,您需要:

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

.


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来自对问题本身的评论。我也有同样的问题,他的评论帮助我自己解决了这个问题。

#8


4  

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 .exit command as input, the user pressing <ctrl>-C twice to signal SIGINT, or by pressing <ctrl>-D to signal 'end' on the input stream. The listener callback is invoked without any arguments.

当REPL通过接收.exit命令作为输入退出时,“退出”事件就会被释放,用户按下 -C两次以发送SIGINT信号,或者按下 -D以在输入流上显示“结束”。侦听器回调是在没有任何参数的情况下调用的。

So to summarize you can exit by:

总结一下,你可以退出:

  1. Typing .exit in nodejs REPL.
  2. 键入.exit in nodejs REPL。
  3. Pressing <ctrl>-C twice.
  4. 按< ctrl > - c两次。
  5. pressing <ctrl>-D.
  6. 按< ctrl > - d。
  7. process.exit(0) meaning a natural exit from REPL. If you want to return any other status you can return a non zero number.
  8. exit(0)表示从REPL中自然退出。如果您想返回任何其他状态,您可以返回一个非零数。
  9. process.kill(process.pid) is the way to kill using nodejs api from within your code or from REPL.
  10. kill(进程。pid)是在代码中或从REPL中使用nodejs api的方法。

#9


2  

If you want to stop your server with npm stop or something like this. You can write the code that kill your server process as:

如果您想要停止您的服务器与npm停止或类似的事情。您可以编写杀死服务器进程的代码:

require('child_process').exec(`kill -9 ${pid}`)

Check this link for the detail: https://gist.github.com/dominhhai/aa7f3314ad27e2c50fd5

查看此链接的详细信息:https://gist.github.com/dominhhai/aa7f3314ad27e2c50fd5。

#10


2  

I ran into an issue where I have multiple node servers running, and I want to just kill one of them and redeploy it from a script.

我遇到了一个问题,我有多个节点服务器在运行,我想要杀死其中一个,然后从脚本中重新部署它。

Note: This example is in a bash shell on Mac.

注意:这个例子在Mac上的bash shell中。

To do so I make sure to make my node call as specific as possible. For example rather than calling node server.js from the apps directory, I call node app_name_1/app/server.js

要做到这一点,我必须确保我的节点调用尽可能具体。例如,而不是调用节点服务器。从应用程序目录中,我调用了node app_name_1/app/server.js。

Then I can kill it using:

然后我可以用:

kill -9 $(ps aux | grep 'node\ app_name_1/app/server.js' | awk '{print $2}')

kill -9 $(ps aux | grep 'node\ app_name_1/app/服务器)。js' | awk '{print $2}'

This will only kill the node process running app_name_1/app/server.js.

这只会杀死运行app_name_1/app/server.js的节点进程。

If you ran node app_name_2/app/server.js this node process will continue to run.

如果运行节点app_name_2/app/服务器。这个节点进程将继续运行。

If you decide you want to kill them all you can use killall node as others have mentioned.

如果您决定要杀死它们,您可以像其他人提到的那样使用killall节点。

#11


2  

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 forever package (from NPM).

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

Example:

Install Forever

npm install forever -g

永远npm安装- g

Run Node Server

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

永远永远的开始过程。/日志/。日志ao。/日志/。日志ae。/日志/犯错。日志server.js

Result:

info: Forever processing file: server.js

信息:永久处理文件:server.js。

Shutdown Node Server

forever stop server.js

永远停止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

信息:永久停止进程:uid命令脚本永远的pid id日志文件uptime [0] sBSj "/usr/bin/nodejs/node" ~/path/to/你/项目/服务器。js 13176 ~ 23084 / .forever /永远。日志0:0:0:0.247

This will cleanly shutdown your Server application.

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