如何将 node.js 服务器作为守护进程启动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4903570/
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 does one start a node.js server as a daemon process?
提问by Jerome WAGNER
In Python Twisted, you have the twistdcommand that helps you with a number of things related to running your application (daemonize it for example).
在 Python Twisted 中,您有一个twistd命令可以帮助您处理与运行应用程序相关的许多事情(例如,对其进行守护程序)。
How do you daemonize a node.jsserver so that it can run even after the current session is closed?
如何守护node.js服务器,使其即使在当前会话关闭后也能运行?
Thanks for your help
谢谢你的帮助
回答by Baggz
Foreveris answer to your question.
永远是你问题的答案。
Install
安装
$ curl https://npmjs.org/install.sh | sh
$ npm install forever
# Or to install as a terminal command everywhere:
$ npm install -g forever
Usage
用法
Using Forever from the command line
从命令行使用 Forever
$ forever start server.js
Using an instance of Forever from Node.js
使用 Node.js 中的 Forever 实例
var forever = require('forever');
var child = new forever.Forever('your-filename.js', {
max: 3,
silent: true,
args: []
});
child.on('exit', this.callback);
child.start();
回答by Budleigh
If you need your process to daemonize itself, not relaying on forever - you can use the daemonizemodule.
如果您需要您的进程自我守护,而不是永远中继 - 您可以使用daemonize模块。
$ npm install daemonize2
Then just write your server file as in example:
然后只需编写您的服务器文件,如示例所示:
var daemon = require("daemonize2").setup({
main: "app.js",
name: "sampleapp",
pidfile: "sampleapp.pid"
});
switch (process.argv[2]) {
case "start":
daemon.start();
break;
case "stop":
daemon.stop();
break;
default:
console.log("Usage: [start|stop]");
}
Mind you, that's rather a low level approach.
请注意,这是一种相当低级的方法。
回答by activedecay
To start a systemdservice manager daemon, write a service file. For example, create a file /etc/systemd/system/myservice.service.
要启动systemd服务管理器守护进程,请编写服务文件。例如,创建一个文件/etc/systemd/system/myservice.service。
[Unit]
Description=myservice-description
After=network.target
[Service]
ExecStart=/opt/myservice-location/src/node/server.js --args=here
Restart=always
User=me
Group=group
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/opt/myservice-location
[Install]
WantedBy=multi-user.target
Remember to update the service manager daemon after every change to the myservice.service file.
请记住在每次更改 myservice.service 文件后更新服务管理器守护程序。
$ systemctl daemon-reload
Then start the service running and enable the service to start at boot.
然后启动服务运行并启用该服务在启动时启动。
$ systemctl start myservice
$ systemctl enable myservice
回答by Hutch
UPDATE: i updated to include the latest from pm2:
更新:我更新为包括来自 pm2 的最新消息:
for many use cases, using a systemd service is the simplest and most appropriate way to manage a node process. for those that are running numerous node processes or independently-running node microservices in a single environment, pm2 is a more full featured tool.
对于许多用例,使用 systemd 服务是管理节点进程的最简单、最合适的方法。对于那些在单个环境中运行多个节点进程或独立运行节点微服务的人来说,pm2 是一个功能更齐全的工具。
https://github.com/unitech/pm2
https://github.com/unitech/pm2
- it has a really useful monitoring feature -> pretty 'gui' for command line monitoring of multiple processes with
pm2 monitor process list withpm2 list - organized Log management ->
pm2 logs - other stuff:
- Behavior configuration
- Source map support
- PaaS Compatible
- Watch & Reload
- Module System
- Max memory reload
- Cluster Mode
- Hot reload
- Development workflow
- Startup Scripts
- Auto completion
- Deployment workflow
- Keymetrics monitoring
- API
- 它有一个非常有用的监控功能 -> 漂亮的“gui”,用于命令行监控多个进程
pm2 monit或进程列表pm2 list - 有组织的日志管理 ->
pm2 logs - 其他的东西:
- 行为配置
- 源地图支持
- PaaS 兼容
- 观看并重新加载
- 模块系统
- 最大内存重载
- 集群模式
- 热重载
- 开发工作流程
- 启动脚本
- 自动完成
- 部署工作流
- 关键指标监控
- 应用程序接口
回答by raidfive
The simplest approach would just to send the command to the background.
最简单的方法就是将命令发送到后台。
$ node server.js &
Then you can kill the process at a later time. I usually do the following:
然后您可以稍后终止该进程。我通常执行以下操作:
$ killall node
Note: I'm running OS X.
注意:我正在运行 OS X。
回答by Lane
You can try:
你可以试试:
$ nohup node server.js &
It work for me on Mac and Linux.
它在 Mac 和 Linux 上对我有用。
The output will be in the ./nohup.outfile
输出将在./nohup.out文件中
But I still recommend you use pm2or forever, because they are easily used for restarting, stopping and logging.
但我仍然建议您使用pm2or forever,因为它们很容易用于重新启动、停止和日志记录。
回答by nponeccop
There are more advanced general-purpose runners, such as monitand runit.
还有更高级的通用运行器,例如monit和runit。
回答by jgmjgm
For the background on the normal way to daemonise on a POSIX system you can search for the C method.
有关在 POSIX 系统上进行守护程序的正常方式的背景知识,您可以搜索 C 方法。
I have not seen enough methods in the node.js API to allow it to be done the C way by hand. However, when using child_process, you can have node.js do it for you:
我在 node.js API 中没有看到足够的方法来允许它手动完成 C 方式。然而,当使用 child_process 时,你可以让 node.js 为你做:
http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
I consider this a potential waste of time because there's a good chance your system provides the same.
我认为这可能是浪费时间,因为您的系统很有可能提供相同的功能。
For example:
例如:
http://libslack.org/daemon/manpages/daemon.1.html
http://libslack.org/daemon/manpages/daemon.1.html
If you want something portable (cross platform) the other posts offer solutions that might suffice.
如果你想要一些便携(跨平台)的东西,其他帖子提供的解决方案可能就足够了。

