如何使 node.js 应用程序永久运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12701259/
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
How to make a node.js application run permanently?
提问by Sam
On a Debian server, I installed Node.js. I understand how to launch an app from putty with this command line:
在 Debian 服务器上,我安装了 Node.js。我了解如何使用以下命令行从腻子启动应用程序:
node /srv/www/MyUserAccount/server/server.js
and get to it on the address 50.51.52.53:8080(IP and port).
并通过地址50.51.52.53:8080(IP 和端口)访问它。
But as soon as I close putty, then I cannot reach the address 50.51.52.53:8080anymore.
但是一旦我关闭腻子,我就无法再访问该地址50.51.52.53:8080。
How to make a Node.js application run permanently?
如何使 Node.js 应用程序永久运行?
As you can guess, I am a beginner with Linux and Node.js.
您可以猜到,我是 Linux 和 Node.js 的初学者。
采纳答案by DogNibbler
Although the other answers solve the OP's problem, they are all overkill and do not explain why he or she is experiencing this issue.
尽管其他答案解决了 OP 的问题,但它们都有些矫枉过正,并没有解释他或她遇到此问题的原因。
The key is this line, "I close putty, then I cannot reach the address"
关键是这一行,“我关闭putty,然后我无法到达地址”
When you are logged into your remote host on Putty you have started an SSH linux process and all commands typed from that SSH session will be executed as children of said process.
当您在 Putty 上登录到远程主机时,您已经启动了一个 SSH linux 进程,并且从该 SSH 会话键入的所有命令都将作为该进程的子进程执行。
Your problem is that when you close Putty you are exiting the SSH session which kills that process and any active child processes. When you close putty you inadvertently kill your server because you ran it in the foreground. To avoid this behavior run the server in the backgroundby appending & to your command:
您的问题是,当您关闭 Putty 时,您正在退出 SSH 会话,这会杀死该进程和任何活动的子进程。当您关闭 putty 时,您无意中杀死了您的服务器,因为您在前台运行它。为了避免这种行为,通过将 & 附加到您的命令在后台运行服务器:
node /srv/www/MyUserAccount/server/server.js &
The problem here is a lack of linux knowledge and not a question about node. For some more info check out: http://linuxconfig.org/understanding-foreground-and-background-linux-processes
这里的问题是缺乏 linux 知识,而不是关于 node.js 的问题。有关更多信息,请查看:http: //linuxconfig.org/understanding-foreground-and-background-linux-processes
UPDATE:
更新:
As others have mentioned, the node server may still die when exiting the terminal. A common gotcha I have come across is that even though the node process is running in bg, it's stdout and stderr is still pointed at the terminal. This means that if the node server writes to console.log or console.error it will receive a broken pipe error and crash. This can be avoided by piping the output of your process:
正如其他人提到的,节点服务器在退出终端时仍然可能死亡。我遇到的一个常见问题是,即使节点进程在 bg 中运行,它的 stdout 和 stderr 仍然指向终端。这意味着如果节点服务器写入console.log 或console.error,它将收到管道损坏错误并崩溃。这可以通过管道输出过程来避免:
node /srv/www/MyUserAccount/server/server.js > stdout.txt 2> stderr.txt &
If the problem persists then you should look into things like tmuxor nohup, which are still more robust than node specific solutions, because they can be used to run all types of processes (databases, logging services, other languages).
如果问题仍然存在,那么您应该研究诸如tmux或nohup之类的东西,它们仍然比特定于节点的解决方案更健壮,因为它们可用于运行所有类型的进程(数据库、日志服务、其他语言)。
A common mistake that could cause the server to exit is that after running the nohup node your_path/server.js &you simply close the Putty terminal by a simple click. You should use exitcommand instead, then your node server will be up and running.
一个可能导致服务器退出的常见错误是,在运行之后,nohup node your_path/server.js &您只需单击一下即可关闭 Putty 终端。您应该改用exit命令,然后您的节点服务器将启动并运行。
回答by N20
You could install foreverusing npm like this:
你可以像这样使用 npm永久安装:
sudo npm install -g forever
And then start your application with:
然后使用以下命令启动您的应用程序:
forever server.js
Or as a service:
或作为服务:
forever start server.js
Forever restarts your app when it crashes or stops for some reason. To restrict restarts to 5 you could use:
Forever 在您的应用程序因某种原因崩溃或停止时重新启动。要将重新启动限制为 5,您可以使用:
forever -m5 server.js
To list all running processes:
列出所有正在运行的进程:
forever list
Note the integer in the brackets and use it as following to stop a process:
请注意括号中的整数,并按如下方式使用它来停止进程:
forever stop 0
Restarting a running process goes:
重新启动正在运行的进程:
forever restart 0
If you're working on your application file, you can use the -wparameter to restart automatically whenever your server.jsfile changes:
如果您正在处理应用程序文件,则可以使用该-w参数在server.js文件更改时自动重新启动:
forever -w server.js
回答by Vikash Rajpurohit
You can use PM2, it's a production process manager for Node.js applications with a built-in load balancer.
您可以使用PM2,它是具有内置负载均衡器的 Node.js 应用程序的生产流程管理器。
Install PM2
安装 PM2
$ npm install pm2 -g
Start an application
启动应用程序
$ pm2 start app.js
If you using express then you can start your app like
如果您使用快递,那么您可以像这样启动您的应用程序
pm2 start ./bin/www --name="app"
Listing all running processes:
列出所有正在运行的进程:
$ pm2 list
It will list all process. You can then stop / restart your service by using ID or Name of the app with following command.
它将列出所有进程。然后,您可以通过以下命令使用应用程序的 ID 或名称来停止/重新启动您的服务。
$ pm2 stop all
$ pm2 stop 0
$ pm2 restart all
To display logs
显示日志
$ pm2 logs ['all'|app_name|app_id]
回答by Brad
I'd recommend looking for something such as Foreverto restart Node in the event of a crash, and handle daemonizing this for you.
我建议寻找诸如Forever 之类的东西,以便在发生崩溃时重新启动 Node,并为您处理守护进程。
回答by film42
If you just want to run your node app in the terminal always, just use screen.
如果您只想始终在终端中运行您的节点应用程序,只需使用 screen.
Install on ubuntu/ debian:
在 ubuntu/debian 上安装:
sudo apt-get install screen
Usage:
用法:
$ screen
$ node /path/to/app.js
ctrl + aand then ctrl + dto dismiss
ctrl + a然后ctrl + d解雇
To get is back:
返回:
One screen: screen -r
一屏: screen -r
If there's more than one you can list all the screens with: screen -ls
如果有多个,您可以列出所有屏幕: screen -ls
And then: screen -r pid_number
进而: screen -r pid_number
回答by Rick Roy
You could simply use this
你可以简单地使用这个
nohup node /srv/www/MyUserAccount/server/server.js &
This will keep the application running and to shut it down you will have to kill it.
这将保持应用程序运行并关闭它,您将不得不杀死它。
For that you could install htopand then search for node and then kill it
为此,您可以安装htop然后搜索节点,然后将其杀死
回答by Rick Roy
Foreveris a very good NodeJs module to do exactly that.
Forever是一个非常好的 NodeJs 模块,可以做到这一点。
Install foreverby typing in the command line
forever在命令行输入安装
$ npm install forever -g
Then use the following command to run a node.js script
然后使用以下命令运行 node.js 脚本
$ forever start /path/to/script.js
You are good to go. Additionally you can run
你已准备好出发。另外你可以运行
$ forever list
to see all the running scripts. You can terminate any specific script by typing
查看所有正在运行的脚本。您可以通过键入终止任何特定脚本
$ forever stop [pid]
where [pid]is the process ID of the script you will obtain from the listcommand. To stop all scripts, you may type
[pid]您将从list命令中获取的脚本的进程 ID在哪里。要停止所有脚本,您可以键入
$ forever stopall
回答by Sudhir singh
Installation
安装
$ [sudo] npm install forever -g
You can use forever to run scripts continuously
您可以使用永远来连续运行脚本
forever start server.js
forever list
for stop service
停止服务
forever stop server.js
回答by Adiii
nohupworking i checked in AWS Ubuntovm follow the correct
syntax
nohup工作我检查了AWS Ubuntovm 遵循正确的
syntax
ubuntu@ip-172-00-00-00:~/ms$ nohup node server.js &
then press enter you will see this line
然后按回车你会看到这一行
ubuntu@ip-172-00-00-00:~/ms$ nohup: ignoring input and appending output to ‘nohup.out'
then type this
然后输入这个
rm nohup.out
回答by Adam Eberlin
Here's an upstartsolution I've been using for my personal projects:
这是我一直用于个人项目的新贵解决方案:
Place it in /etc/init/node_app_daemon.conf:
把它放在/etc/init/node_app_daemon.conf:
description "Node.js Daemon"
author "Adam Eberlin"
stop on shutdown
respawn
respawn limit 3 15
script
export APP_HOME="/srv/www/MyUserAccount/server"
cd $APP_HOME
exec sudo -u user /usr/bin/node server.js
end script
This will also handle respawning your application in the event that it crashes. It will give up attempts to respawn your application if it crashes 3 or more times in less than 15 seconds.
这也将在应用程序崩溃时处理重新生成应用程序。如果应用程序在 15 秒内崩溃 3 次或更多次,它将放弃重新生成应用程序的尝试。


