从命令行停止node.js程序

本文翻译自:Stop node.js program from command line

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. 我用node server.js启动它,然后在Mac上用Ctrl + Z关闭它。 When I try to run it again with node server.js I get this error message: 当我尝试使用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? 我怎样才能防止这种情况发生?


#1楼

参考:https://stackoom.com/question/I9og/从命令行停止node-js程序


#2楼

To end the program, you should be using Ctrl + C . 要结束程序,您应该使用Ctrl + C. If you do that, it sends SIGINT , which allows the program to end gracefully, unbinding from any ports it is listening on. 如果你这样做,它会发送SIGINT ,它允许程序正常结束,从正在侦听的任何端口解除绑定。

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


#3楼

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

#4楼

$ sudo killall node in another terminal works on mac , while killall node not working: 另一个终端中的$ sudo killall nodemac上工作 ,而killall node不工作:

$ killall node
No matching processes belonging to you were found

#5楼

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


#6楼

If you are running Node.js interactively (the REPL): 如果您以交互方式运行Node.js(REPL):

Ctrl + C will take back you to > prompt then type: Ctrl + C将收回>提示然后键入:

process.exit()

or just use Ctrl + D . 或者只使用Ctrl + D.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值