When we exit command prompt when npm start is still running (Node.js), will the process end and the server stop?

StackOverflow https://stackoverflow.com/questions/23645085

I'm new to node and don't really know how things will turn out.

有帮助吗?

解决方案

The short answer is: yes, the process will end.

When you run a process in a command prompt (or terminal), then the command processor basically pauses while your process is running. When you shut down the command prompt, you kill whatever process is running in that prompt.

On Linux systems, there's a couple ways of keeping the program around when killing the terminal:

node server.js &

The & tells the terminal to run the job in the background. You'll notice that it returns you immediately to a prompt.

However, when you actually log out, your process will get killed. In order to prevent that, you can use the nohup command (as indicated in the comments by @YTowOnt9):

nohup node server.js &

This tells the process to ignore the HUP (hangup) signal:

From Wikipedia:

nohup is a POSIX command to ignore the HUP (hangup) signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout.

On Windows system, you can use the START command, but as far as I know there's no way to keep the program running after logging out short of turning the program into a Windows Service (which is a whole other topic):

start node server.js

If you want to prevent the creation of a new window:

start /b node server.js
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top