Stop node.js program from command line

2019-01-15 23:30发布

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

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:

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?

14条回答
对你真心纯属浪费
2楼-- · 2019-01-16 00:07

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

$ killall node
No matching processes belonging to you were found
查看更多
Ridiculous、
3楼-- · 2019-01-16 00:08

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

Example:

Install Forever

npm install forever -g

Run Node Server

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

Result:

info: Forever processing file: server.js

Shutdown Node Server

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

This will cleanly shutdown your Server application.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-16 00:12

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

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.

So to summarize you can exit by:

  1. Typing .exit in nodejs REPL.
  2. Pressing <ctrl>-C twice.
  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.
查看更多
我想做一个坏孩纸
5楼-- · 2019-01-16 00:14

on linux try: pkill node

on windows:

Taskkill /IM node.exe /F

or

from subprocess import call

call(['taskkill', '/IM', 'node.exe', '/F'])
查看更多
Rolldiameter
6楼-- · 2019-01-16 00:17

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.

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

Then I can kill it using:

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

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

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

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

查看更多
太酷不给撩
7楼-- · 2019-01-16 00:20

you can type .exit to quit node js REPL

查看更多
登录 后发表回答