如何将 Node.js 应用程序作为后台服务运行?

发布于 2024-09-28 23:54:17 字数 865 浏览 7 评论 0 原文

由于多年来这篇文章引起了很多关注,我在这篇文章的底部列出了每个平台的顶级解决方案。


原帖

我希望我的node.js 服务器在后台运行,即:当我关闭终端时,我希望我的服务器继续运行。我用谷歌搜索了这个并想出了这个教程,但是它不起作用正如预期的那样。因此,我没有使用该守护程序脚本,而是认为我只是使用了输出重定向(2>&1>>文件部分),但这也不会退出 - 我得到一个空行在我的终端中,就像它正在等待输出/错误一样。

我还尝试将进程置于后台,但是一旦我关闭终端,该进程也会被终止。

那么当我关闭本地计算机时如何让它继续运行呢?


顶级解决方案

Since this post has gotten a lot of attention over the years, I've listed the top solutions per platform at the bottom of this post.


Original post:

I want my node.js server to run in the background, i.e.: when I close my terminal I want my server to keep running. I've googled this and came up with this tutorial, however it doesn't work as intended. So instead of using that daemon script, I thought I just used the output redirection (the 2>&1 >> file part), but this too does not exit - I get a blank line in my terminal, like it's waiting for output/errors.

I've also tried to put the process in the background, but as soon as I close my terminal the process is killed as well.

So how can I leave it running when I shut down my local computer?


Top solutions:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

荒芜了季节 2024-10-05 23:54:18

复制我自己的答案我应该将 Node.js 应用程序作为自己的进程运行吗?

2015 年答案:几乎每个 Linux 发行版都附带 systemd,这意味着 forever、monit、PM2 等不再是必要的 - 您的操作系统已经处理这些任务

创建一个 myapp.service 文件(显然,将“myapp”替换为您的应用程序名称):

[Unit]
Description=My app

[Service]
ExecStart=/var/www/myapp/app.js
Restart=always
User=nobody
# Note Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/myapp

[Install]
WantedBy=multi-user.target

如果您是 Unix 新手,请注意: /var/www/ myapp/app.js 应该在第一行有 #!/usr/bin/env 节点 并打开可执行模式 chmod +x app.js.

将您的服务文件复制到 /etc/systemd/system 中。

使用systemctl start myapp启动它。

使用 systemctl enable myapp 使其能够在启动时运行。

使用 journalctl -u myapp 查看日志

这取自我们如何在 Linux 2018 版上部署节点应用程序,其中还包括生成 AWS/DigitalOcean/Azure CloudConfig 来构建 Linux/ 的命令/节点服务器(包括 .service 文件)。

Copying my own answer from How do I run a Node.js application as its own process?

2015 answer: nearly every Linux distro comes with systemd, which means forever, monit, PM2, etc are no longer necessary - your OS already handles these tasks.

Make a myapp.service file (replacing 'myapp' with your app's name, obviously):

[Unit]
Description=My app

[Service]
ExecStart=/var/www/myapp/app.js
Restart=always
User=nobody
# Note Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/myapp

[Install]
WantedBy=multi-user.target

Note if you're new to Unix: /var/www/myapp/app.js should have #!/usr/bin/env node on the very first line and have the executable mode turned on chmod +x app.js.

Copy your service file into the /etc/systemd/system.

Start it with systemctl start myapp.

Enable it to run on boot with systemctl enable myapp.

See logs with journalctl -u myapp

This is taken from How we deploy node apps on Linux, 2018 edition, which also includes commands to generate an AWS/DigitalOcean/Azure CloudConfig to build Linux/node servers (including the .service file).

吝吻 2024-10-05 23:54:18

更新 - 正如下面的一个答案中提到的,PM2 有一些非常好的功能永远缺失。考虑使用它。

原始答案

使用nohup

nohup node server.js &

编辑我想补充一点,接受的答案确实是要走的路。我在需要保持运行状态的实例上使用forever。我喜欢执行 npm install -g permanent 因此它位于节点路径中,然后执行 forever start server.js

UPDATE - As mentioned in one of the answers below, PM2 has some really nice functionality missing from forever. Consider using it.

Original Answer

Use nohup:

nohup node server.js &

EDIT I wanted to add that the accepted answer is really the way to go. I'm using forever on instances that need to stay up. I like to do npm install -g forever so it's in the node path and then just do forever start server.js

醉城メ夜风 2024-10-05 23:54:18

您可以使用 Forever,这是一个简单的 CLI 工具,用于确保给定的节点脚本连续运行(即永远):
https://www.npmjs.org/package/forever

You can use Forever, A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever):
https://www.npmjs.org/package/forever

〃温暖了心ぐ 2024-10-05 23:54:18

这可能不是公认的方式,但我用屏幕来做到这一点,特别是在开发过程中,因为我可以在必要时将其恢复并愚弄它。

screen
node myserver.js
>>CTRL-A then hit D

屏幕将分离并在您注销后继续存在。然后你可以通过 screen -r 将其恢复。点击屏幕手册了解更多详细信息。如果您愿意,您可以为屏幕命名等等。

This might not be the accepted way, but I do it with screen, especially while in development because I can bring it back up and fool with it if necessary.

screen
node myserver.js
>>CTRL-A then hit D

The screen will detach and survive you logging off. Then you can get it back back doing screen -r. Hit up the screen manual for more details. You can name the screens and whatnot if you like.

揪着可爱 2024-10-05 23:54:18

2016 年更新:
node-windows/mac/linux系列使用跨所有操作系统的通用API,因此它绝对是一个相关的解决方案。然而; node-linux 生成 systemv 初始化文件。随着 systemd 的不断普及,它实际上是 Linux 上更好的选择。如果有人想为 node-linux 添加 systemd 支持,欢迎 PR :-)

原始线程:

这是一个非常旧的线程,但是 node-windows 提供了另一种在 Windows 上创建后台服务的方法。它大致基于在节点脚本周围使用 exe 包装器的 nssm 概念。然而;它使用 winsw.exe 来代替,并提供一个可配置的节点包装器,以便更精细地控制进程在失败时如何启动/停止。这些进程与任何其他服务一样可用:

在此处输入图像描述

该模块还包含一些事件日志记录:

在此处输入图像描述

守护进程化脚本是通过代码完成的。例如:

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'Hello World',
  description: 'The nodejs.org example web server.',
  script: 'C:\\path\\to\\my\\node\\script.js'
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

// Listen for the "start" event and let us know when the
// process has actually started working.
svc.on('start',function(){
  console.log(svc.name+' started!\nVisit http://127.0.0.1:3000 to see it in action.');
});

// Install the script as a service.
svc.install();

该模块支持诸如限制重新启动(因此糟糕的脚本不会破坏您的服务器)和增加重新启动之间的时间间隔等功能。

由于节点窗口服务与其他服务一样运行,因此可以使用您已经使用的任何软件来管理/监视服务。

最后,没有 make 依赖项。换句话说,简单的 npm install -g node-windows 就可以了。您不需要 Visual Studio、.NET 或 node-gyp magic 来安装它。此外,它还获得了 MIT 和 BSD 许可。

坦白说,我是这个模块的作者。它旨在减轻 OP 所经历的确切痛苦,但更紧密地集成到操作系统已提供的功能中。我希望未来有同样问题的观众会发现它很有用。

2016 Update:
The node-windows/mac/linux series uses a common API across all operating systems, so it is absolutely a relevant solution. However; node-linux generates systemv init files. As systemd continues to grow in popularity, it is realistically a better option on Linux. PR's welcome if anyone wants to add systemd support to node-linux :-)

Original Thread:

This is a pretty old thread now, but node-windows provides another way to create background services on Windows. It is loosely based on the nssm concept of using an exe wrapper around your node script. However; it uses winsw.exe instead and provides a configurable node wrapper for more granular control over how the process starts/stops on failures. These processes are available like any other service:

enter image description here

The module also bakes in some event logging:

enter image description here

Daemonizing your script is accomplished through code. For example:

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'Hello World',
  description: 'The nodejs.org example web server.',
  script: 'C:\\path\\to\\my\\node\\script.js'
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

// Listen for the "start" event and let us know when the
// process has actually started working.
svc.on('start',function(){
  console.log(svc.name+' started!\nVisit http://127.0.0.1:3000 to see it in action.');
});

// Install the script as a service.
svc.install();

The module supports things like capping restarts (so bad scripts don't hose your server) and growing time intervals between restarts.

Since node-windows services run like any other, it is possible to manage/monitor the service with whatever software you already use.

Finally, there are no make dependencies. In other words, a straightforward npm install -g node-windows will work. You don't need Visual Studio, .NET, or node-gyp magic to install this. Also, it's MIT and BSD licensed.

In full disclosure, I'm the author of this module. It was designed to relieve the exact pain the OP experienced, but with tighter integration into the functionality the Operating System already provides. I hope future viewers with this same question find it useful.

酒绊 2024-10-05 23:54:18

如果您只是想不间断地运行脚本直到完成,您可以使用 nohup (如此处答案中已提到的)。但是,没有一个答案提供同时记录 stdin 和 stdout 的完整命令。

nohup node index.js >> app.log 2>&1 &
  • >> 表示附加到 app.log
  • 2>&1 确保错误也发送到 stdout 并添加到 app.log 中。
  • 结尾的 & 确保您当前的终端与命令断开连接,以便您可以继续工作。

如果您想运行节点服务器(或者在服务器重新启动时应该启动备份的东西),您应该使用 systemd / systemctl

If you simply want to run the script uninterrupted until it completes you can use nohup as already mentioned in the answers here. However, none of the answers provide a full command that also logs stdin and stdout.

nohup node index.js >> app.log 2>&1 &
  • The >> means append to app.log.
  • 2>&1 makes sure that errors are also send to stdout and added to the app.log.
  • The ending & makes sure your current terminal is disconnected from command so you can continue working.

If you want to run a node server (or something that should start back up when the server restarts) you should use systemd / systemctl.

執念 2024-10-05 23:54:18

更新:我更新了 pm2 的最新内容:

对于许多用例,使用 systemd 服务是管理节点进程的最简单、最合适的方法。对于那些在单一环境中运行大量节点进程或独立运行的节点微服务的人来说,pm2 是一个功能更齐全的工具。

https://github.com/unitech/pm2

http://pm2.io

  • 它有一个非常有用的监控功能 ->漂亮的“gui”,用于使用 pm2 monit 命令行监控多个进程,或使用 pm2 list
  • 组织日志管理进程列表 -> pm2 记录
  • 其他内容:

    <块引用>

    • 行为配置
    • 源地图支持
    • 兼容 PaaS
    • 观看与观看重新加载
    • 模块系统
    • 最大内存重新加载
    • 集群模式
    • 热重载
    • 开发工作流程
    • 启动脚本
    • 自动完成
    • 部署工作流程
    • 关键指标监控
    • API

UPDATE: i updated to include the latest from 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.

https://github.com/unitech/pm2

http://pm2.io

  • it has a really useful monitoring feature -> pretty 'gui' for command line monitoring of multiple processes with pm2 monit or process list with pm2 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
痴梦一场 2024-10-05 23:54:18

如果您正在使用nohup,请尝试运行此命令 -

nohup npm start 2>/dev/null 1>/dev/null&

您也可以使用forever来启动服务器

forever start -c "npm start" ./ 

PM2也支持npm start

pm2 start npm -- start

Try to run this command if you are using nohup -

nohup npm start 2>/dev/null 1>/dev/null&

You can also use forever to start server

forever start -c "npm start" ./ 

PM2 also supports npm start

pm2 start npm -- start
许一世地老天荒 2024-10-05 23:54:18

如果您运行的是 OSX,那么生成真正的系统进程的最简单方法是使用 launchd 来启动它。

构建一个像这样的 plist,并将其放入 /Library/LaunchDaemons 中,名称为 top-level-domain.your-domain.application.plist (放置时您需要是 root)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>top-level-domain.your-domain.application</string>

    <key>WorkingDirectory</key>
    <string>/your/preferred/workingdirectory</string>

    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/node</string>
        <string>your-script-file</string>
    </array>

    <key>RunAtLoad</key>
    <true/>

    <key>KeepAlive</key>
    <true/>

</dict>
</plist>

:完成后,发出此命令(以 root 身份):

launchctl load /Library/LaunchDaemons/top-level-domain.your-domain.application.plist
launchctl start top-level-domain.your-domain.application

然后您就可以运行了。

重新启动后您仍将运行。

有关 plist 中的其他选项,请查看此处的手册页: https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html

If you are running OSX, then the easiest way to produce a true system process is to use launchd to launch it.

Build a plist like this, and put it into the /Library/LaunchDaemons with the name top-level-domain.your-domain.application.plist (you need to be root when placing it):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>top-level-domain.your-domain.application</string>

    <key>WorkingDirectory</key>
    <string>/your/preferred/workingdirectory</string>

    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/node</string>
        <string>your-script-file</string>
    </array>

    <key>RunAtLoad</key>
    <true/>

    <key>KeepAlive</key>
    <true/>

</dict>
</plist>

When done, issue this (as root):

launchctl load /Library/LaunchDaemons/top-level-domain.your-domain.application.plist
launchctl start top-level-domain.your-domain.application

and you are running.

And you will still be running after a restart.

For other options in the plist look at the man page here: https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html

嘿看小鸭子会跑 2024-10-05 23:54:18

我只是使用 daemon npm 模块:

var daemon = require('daemon');

daemon.daemonize({
    stdout: './log.log'
  , stderr: './log.error.log'
  }
, './node.pid'
, function (err, pid) {
  if (err) {
    console.log('Error starting daemon: \n', err);
    return process.exit(-1);
  }
  console.log('Daemonized successfully with pid: ' + pid);

  // Your Application Code goes here
});

最近我也在使用 mon(1)< /a> 来自 TJ Holowaychuk 用于启动和管理简单的节点应用程序。

I am simply using the daemon npm module:

var daemon = require('daemon');

daemon.daemonize({
    stdout: './log.log'
  , stderr: './log.error.log'
  }
, './node.pid'
, function (err, pid) {
  if (err) {
    console.log('Error starting daemon: \n', err);
    return process.exit(-1);
  }
  console.log('Daemonized successfully with pid: ' + pid);

  // Your Application Code goes here
});

Lately I'm also using mon(1) from TJ Holowaychuk to start and manage simple node apps.

任谁 2024-10-05 23:54:18

我使用 Supervisor 进行开发。它就是有效的。当您对 .js 文件进行更改时,Supervisor 会自动重新启动您的应用程序并加载这些更改。

这是其 Github 页面的链接

安装:

sudo npm install Supervisor -g

您可以使用 -e 轻松使其监视其他扩展。我经常使用的另一个命令是 -i 来忽略某些文件夹。

您可以使用 nohup 和 Supervisor 使您的节点应用程序在后台运行,即使在您注销后也是如此。

sudo nohup Supervisor myapp.js &

I use Supervisor for development. It just works. When ever you make changes to a .js file Supervisor automatically restarts your app with those changes loaded.

Here's a link to its Github page

Install :

sudo npm install supervisor -g

You can easily make it watch other extensions with -e. Another command I use often is -i to ignore certain folders.

You can use nohup and supervisor to make your node app run in the background even after you log out.

sudo nohup supervisor myapp.js &

方觉久 2024-10-05 23:54:18

Node.js 作为 WINDOWS XP 中的后台服务

安装:

  1. 通过安装程序可执行文件安装 WGET http://gnuwin32.sourceforge.net/packages/wget.htm
  2. 安装 GIT http://code.google.com/p/msysgit/downloads/list 通过安装程序可执行文件
  3. 安装 NSSM http://nssm.cc/download/?page=download 通过将 nnsm.exe 复制到 %windir%/system32文件夹
  4. 创建c:\node\helloworld.js

    // http://howtonode.org/hello-node
    var http = require('http');
    var server = http.createServer(function (请求, 响应) {
        response.writeHead(200, {"Content-Type": "text/plain"});
        响应.end("你好世界\n");
    });
    服务器.听(8000);
    console.log("服务器运行在http://127.0.0.1:8000/");
    
  5. 打开命令控制台并键入以下内容(仅当安装了资源工具包时才使用 setx)

    C:\node>设置路径=%PATH%;%CD%
    C:\节点> setx 路径“%PATH%”
    C:\节点>设置 NODE_PATH="C:\Program Files\nodejs\node_modules"
    C:\节点> git config --system http.sslcainfo /bin/curl-ca-bundle.crt    
    C:\节点> git clone --recursive git://github.com/isaacs/npm.git    
    C:\节点> cd npm    
    C:\node\npm> Node cli.js 安装 npm -gf   
    C:\节点>光盘 ..    
    C:\节点> nssm.exe 安装node-helloworld "C:\Program Files\nodejs\node.exe" c:\node\helloworld.js    
    C:\节点>网络启动节点-helloworld
    
  6. 一个漂亮的批处理好东西是创建 c:\node\ServiceMe.cmd

    @echo 关闭
    nssm.exe 安装节点-%~n1 "C:\Program Files\nodejs\node.exe" %~s1
    网络起始节点-%~n1
    暂停
    

服务管理:

  • 现在可以通过“开始”->“服务”访问服务本身。运行->
    services.msc 或通过“开始”->“运行”->“ MSCONFIG->服务(并选中“隐藏
    所有 Microsoft 服务')。
  • 该脚本将为通过批处理脚本创建的每个节点添加前缀
    '节点-'。
  • 同样,它们可以在注册表中找到:“HKLM\SYSTEM\CurrentControlSet\Services\node-xxxx

Node.js as a background service in WINDOWS XP

Installation:

  1. Install WGET http://gnuwin32.sourceforge.net/packages/wget.htm via installer executable
  2. Install GIT http://code.google.com/p/msysgit/downloads/list via installer executable
  3. Install NSSM http://nssm.cc/download/?page=download via copying nnsm.exe into %windir%/system32 folder
  4. Create c:\node\helloworld.js

    // http://howtonode.org/hello-node
    var http = require('http');
    var server = http.createServer(function (request, response) {
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.end("Hello World\n");
    });
    server.listen(8000);
    console.log("Server running at http://127.0.0.1:8000/");
    
  5. Open command console and type the following (setx only if Resource Kit is installed)

    C:\node> set path=%PATH%;%CD%
    C:\node> setx path "%PATH%"
    C:\node> set NODE_PATH="C:\Program Files\nodejs\node_modules"
    C:\node> git config --system http.sslcainfo /bin/curl-ca-bundle.crt    
    C:\node> git clone --recursive git://github.com/isaacs/npm.git    
    C:\node> cd npm    
    C:\node\npm> node cli.js install npm -gf   
    C:\node> cd ..    
    C:\node> nssm.exe install node-helloworld "C:\Program Files\nodejs\node.exe" c:\node\helloworld.js    
    C:\node> net start node-helloworld
    
  6. A nifty batch goodie is to create c:\node\ServiceMe.cmd

    @echo off
    nssm.exe install node-%~n1 "C:\Program Files\nodejs\node.exe" %~s1
    net start node-%~n1
    pause
    

Service Management:

  • The services themselves are now accessible via Start-> Run->
    services.msc or via Start->Run-> MSCONFIG-> Services (and check 'Hide
    All Microsoft Services').
  • The script will prefix every node made via the batch script with
    'node-'.
  • Likewise they can be found in the registry: "HKLM\SYSTEM\CurrentControlSet\Services\node-xxxx"
万劫不复 2024-10-05 23:54:18

接受的答案可能是最好的生产答案,但是对于进行开发工作的快速破解,我发现:

nodejs scriptname.js & 不起作用,因为 nodejs 似乎吞噬了 & ,所以这件事不允许我继续使用终端而不导致 scriptname.js 死亡。

但我将 nodejs scriptname.js 放在 .sh 文件中,并且
nohup sh startscriptname.sh & 有效。

绝对不是生产问题,但它解决了“我需要继续使用我的终端并且不想启动 5 个不同的终端”问题。

The accepted answer is probably the best production answer, but for a quick hack doing dev work, I found this:

nodejs scriptname.js & didn't work, because nodejs seemed to gobble up the &, and so the thing didn't let me keep using the terminal without scriptname.js dying.

But I put nodejs scriptname.js in a .sh file, and
nohup sh startscriptname.sh & worked.

Definitely not a production thing, but it solves the "I need to keep using my terminal and don't want to start 5 different terminals" problem.

原野 2024-10-05 23:54:18

2017 年 6 月更新:
Linux 的解决方案:(红帽)。以前的评论对我不起作用。
这对我在 Amazon Web Service - Red Hat 7 上有用。希望这对其他人有用。

A. Create the service file 
sudo vi /etc/systemd/system/myapp.service
[Unit]
Description=Your app
After=network.target

[Service]
ExecStart=/home/ec2-user/meantodos/start.sh
WorkingDirectory=/home/ec2-user/meantodos/

[Install]
WantedBy=multi-user.target

B. Create a shell file
/home/ec2-root/meantodos/start.sh
#!/bin/sh -
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
npm start

then:
chmod +rx /home/ec2-root/meantodos/start.sh
(to make this file executable)

C. Execute the Following

sudo systemctl daemon-reload
sudo systemctl start myapp
sudo systemctl status myapp

(If there are no errors, execute below.  Autorun after server restarted.)
chkconfig myapp -add

June 2017 Update:
Solution for Linux: (Red hat). Previous comments doesn't work for me.
This works for me on Amazon Web Service - Red Hat 7. Hope this works for somebody out there.

A. Create the service file 
sudo vi /etc/systemd/system/myapp.service
[Unit]
Description=Your app
After=network.target

[Service]
ExecStart=/home/ec2-user/meantodos/start.sh
WorkingDirectory=/home/ec2-user/meantodos/

[Install]
WantedBy=multi-user.target

B. Create a shell file
/home/ec2-root/meantodos/start.sh
#!/bin/sh -
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
npm start

then:
chmod +rx /home/ec2-root/meantodos/start.sh
(to make this file executable)

C. Execute the Following

sudo systemctl daemon-reload
sudo systemctl start myapp
sudo systemctl status myapp

(If there are no errors, execute below.  Autorun after server restarted.)
chkconfig myapp -add
卸妝后依然美 2024-10-05 23:54:18

如果你在linux服务器上运行nodejs,我认为这是最好的方法。

创建服务脚本并复制到 /etc/init/nodejs.conf

启动服务: sudo service nodejs start

停止服务: sudo service nodejs stop

服务脚本

description "DManager node.js server - Last Update: 2012-08-06"
author      "Pedro Muniz - [email protected]"

env USER="nodejs" #you have to create this user 
env APPNAME="nodejs" #you can change the service name
env WORKDIR="/home/<project-home-dir>" #set your project home folder here
env COMMAND="/usr/bin/node <server name>" #app.js ?

# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

pre-start script
    sudo -u $USER echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/$APPNAME.log
end script

script
    # Not sure why $HOME is needed, but we found that it is:
    export HOME="<project-home-dir>"  #set your project home folder here
    export NODE_PATH="<project node_path>"

    #log file, grant permission to nodejs user
    exec start-stop-daemon --start --make-pidfile --pidfile /var/run/$APPNAME.pid --chuid $USER --chdir $WORKDIR --exec $COMMAND >> /var/log/$APPNAME.log 2>&1
end script

post-start script
   # Optionally put a script here that will notifiy you node has (re)started
   # /root/bin/hoptoad.sh "node.js has started!"
end script

pre-stop script
    sudo -u $USER echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/$APPNAME.log
end script

If you are running nodejs in linux server, I think this is the best way.

Create a service script and copy to /etc/init/nodejs.conf

start service: sudo service nodejs start

stop service: sudo service nodejs stop

Sevice script

description "DManager node.js server - Last Update: 2012-08-06"
author      "Pedro Muniz - [email protected]"

env USER="nodejs" #you have to create this user 
env APPNAME="nodejs" #you can change the service name
env WORKDIR="/home/<project-home-dir>" #set your project home folder here
env COMMAND="/usr/bin/node <server name>" #app.js ?

# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

pre-start script
    sudo -u $USER echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/$APPNAME.log
end script

script
    # Not sure why $HOME is needed, but we found that it is:
    export HOME="<project-home-dir>"  #set your project home folder here
    export NODE_PATH="<project node_path>"

    #log file, grant permission to nodejs user
    exec start-stop-daemon --start --make-pidfile --pidfile /var/run/$APPNAME.pid --chuid $USER --chdir $WORKDIR --exec $COMMAND >> /var/log/$APPNAME.log 2>&1
end script

post-start script
   # Optionally put a script here that will notifiy you node has (re)started
   # /root/bin/hoptoad.sh "node.js has started!"
end script

pre-stop script
    sudo -u $USER echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/$APPNAME.log
end script
命比纸薄 2024-10-05 23:54:18

我有点晚了。简单的解决方案是在终端中执行以下两个命令,如下所示。

nohup node ./server.js &
disown -h %1 

然后关闭 ssh 会话,节点程序继续运行。在 Ubuntu 18 上测试。

I am bit late. Simple solution is to execute below two commands in terminal as shown below.

nohup node ./server.js &
disown -h %1 

Then close your ssh session and node program continues to run. Tested on Ubuntu 18.

來不及說愛妳 2024-10-05 23:54:18

使用 nssm Windows 最佳解决方案,只需下载 nssm,打开 cmd 到 nssm 目录并键入

nssm install <service name> <node path> <app.js path> 

eg: nssm install myservice "C:\Program Files\nodejs" "C:\myapp\app.js" 

此命令将安装一个新的 Windows 服务,该服务将在 services.msc 中列出,您可以从那里启动或停止该服务,该服务将自动启动,您可以配置在失败时重新启动。

use nssm the best solution for windows, just download nssm, open cmd to nssm directory and type

nssm install <service name> <node path> <app.js path> 

eg: nssm install myservice "C:\Program Files\nodejs" "C:\myapp\app.js" 

this will install a new windows service which will be listed at services.msc from there you can start or stop the service, this service will auto start and you can configure to restart if it fails.

酒废 2024-10-05 23:54:18

使用pm2模块。 pm2 Nodejs 模块

Use pm2 module. pm2 nodejs module

装迷糊 2024-10-05 23:54:18

由于我在提供的答案列表中缺少此选项,因此我想在 2020 年添加一个符合条件的选项: docker 或任何同等内容 容器平台。除了确保您的应用程序在稳定的环境中运行之外,还有其他安全优势以及改进的可移植性。

Windows、macOS 和大多数/主要 Linux 发行版都支持 docker。 在受支持的平台上安装 docker 相当简单且有详细记录。设置 Node.js 应用程序非常简单,只需将其放入容器中并运行该容器,同时确保其在关闭后重新启动。

创建容器映像

假设您的应用程序在该服务器上的 /home/me/my-app 中可用,请创建一个文本文件 Dockerfile 位于文件夹 /home/me 中,内容与此类似:

FROM node:lts-alpine
COPY /my-app/ /app/
RUN cd /app && npm ci
CMD ["/app/server.js"]

它正在创建一个用于运行的映像Alpine Linux 下 Node.js 的 LTS 版本,将应用程序的文件复制到映像中并运行 npm ci 以确保依赖项与运行时上下文匹配。

在包含内容的同一文件夹中创建另一个文件 .dockerignore

**/node_modules

这将防止主机系统的现有依赖项注入到容器中,因为它们可能无法在容器中工作。 Dockerfile 中提供的 RUN 命令将解决这个问题。

使用如下命令创建镜像:

docker build -t myapp-as-a-service /home/me

-t 选项选择构建容器镜像的“名称”。这用于下面运行的容器。

注意:最后一个参数是选择包含该 Dockerfile 的文件夹而不是 Dockerfile 本身。您可以使用选项 -f 选择不同的选项。

启动容器

使用此命令启动容器:

docker run -d --restart always -p 80:3000 myapp-as-a-service

此命令假设您的应用程序正在侦听端口 3000,并且您希望它在主机的端口 80 上公开。

这当然是一个非常有限的例子,但它是一个很好的起点。

Since I'm missing this option in the list of provided answers I'd like to add an eligible option as of 2020: docker or any equivalent container platform. In addition to ensuring your application is working in a stable environment there are additional security benefits as well as improved portability.

There is docker support for Windows, macOS and most/major Linux distributions. Installing docker on a supported platform is rather straight-forward and well-documented. Setting up a Node.js application is as simple as putting it in a container and running that container while making sure its being restarted after shutdown.

Create Container Image

Assuming your application is available in /home/me/my-app on that server, create a text file Dockerfile in folder /home/me with content similar to this one:

FROM node:lts-alpine
COPY /my-app/ /app/
RUN cd /app && npm ci
CMD ["/app/server.js"]

It is creating an image for running LTS version of Node.js under Alpine Linux, copying the application's files into the image and runs npm ci to make sure dependencies are matching that runtime context.

Create another file .dockerignore in same folder with content

**/node_modules

This will prevent existing dependencies of your host system from being injected into container as they might not work there. The presented RUN command in Dockerfile is going to fix that.

Create the image using command like this:

docker build -t myapp-as-a-service /home/me

The -t option is selecting the "name" of built container image. This is used on running containers below.

Note: Last parameter is selecting folder containing that Dockerfile instead of the Dockerfile itself. You may pick a different one using option -f.

Start Container

Use this command for starting the container:

docker run -d --restart always -p 80:3000 myapp-as-a-service

This command is assuming your app is listening on port 3000 and you want it to be exposed on port 80 of your host.

This is a very limited example for sure, but it's a good starting point.

寒尘 2024-10-05 23:54:18

为了完善建议的各种选项,这里还有一个:GNU/Linux 中的 daemon 命令,您可以在此处阅读:http://libslack.org/daemon/manpages/daemon.1.html。 (如果上面的评论之一已经提到了这一点,我们深表歉意)。

To round out the various options suggested, here is one more: the daemon command in GNU/Linux, which you can read about here: http://libslack.org/daemon/manpages/daemon.1.html. (apologies if this is already mentioned in one of the comments above).

赠我空喜 2024-10-05 23:54:18

看看赋格!除了启动许多工作人员之外,您还可以妖魔化您的节点进程!

http://github.com/pgte/fugue

Check out fugue! Apart from launching many workers, you can demonize your node process too!

http://github.com/pgte/fugue

与酒说心事 2024-10-05 23:54:18

PM2 是 Node.js 应用程序的生产流程管理器,具有内置负载均衡器。它允许您使应用程序永远保持活动状态,无需停机即可重新加载它们,并简化常见的系统管理任务。
https://github.com/Unitech/pm2

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
https://github.com/Unitech/pm2

情何以堪。 2024-10-05 23:54:18

我很惊讶没有人提到 Guvnor

我一直在尝试,pm2 等。但是,当它出现时对于可靠的控制和基于网络的性能指标,我发现 Guvnor 是迄今为止最好的。另外,它也是完全开源的。

输入图片此处描述

编辑:但是,我不确定它是否适用于 Windows。我只在 Linux 上使用过它。

I am surprised that nobody has mentioned Guvnor

I have tried forever, pm2, etc. But, when it comes to solid control and web based performance metrics, I have found Guvnor to be by far the best. Plus, it is also fully opensource.

enter image description here

Edit : However, I am not sure if it works on windows. I've only used it on linux.

拿命拼未来 2024-10-05 23:54:18

有人注意到“2>&1”的位置有一个小错误吗?

2>&1 >> file

应该是

>> file 2>&1

has anyone noticed a trivial mistaken of the position of "2>&1" ?

2>&1 >> file

should be

>> file 2>&1
将军与妓 2024-10-05 23:54:18

我将 tmux 用于远程主机上的多窗口/窗格开发环境。分离并保持进程在后台运行非常简单。看看 tmux

I use tmux for a multiple window/pane development environment on remote hosts. It's really simple to detach and keep the process running in the background. Have a look at tmux

幻梦 2024-10-05 23:54:18

对于使用较新版本的 daemon npm 模块的人 - 您需要传递文件描述符而不是字符串:

var fs = require('fs');
var stdoutFd = fs.openSync('output.log', 'a');
var stderrFd = fs.openSync('errors.log', 'a');
require('daemon')({
    stdout: stdoutFd, 
    stderr: stderrFd
});

For people using newer versions of the daemon npm module - you need to pass file descriptors instead of strings:

var fs = require('fs');
var stdoutFd = fs.openSync('output.log', 'a');
var stderrFd = fs.openSync('errors.log', 'a');
require('daemon')({
    stdout: stdoutFd, 
    stderr: stderrFd
});
幸福还没到 2024-10-05 23:54:18

如果您使用 pm2,则可以将 autorestart 设置为 false 来使用它:

$ pm2 生态系统

这将生成一个示例 ecosystem.config.js

module.exports = {
  apps: [
    {
      script: './scripts/companies.js',
      autorestart: false,
    },
    {
      script: './scripts/domains.js',
      autorestart: false,
    },
    {
      script: './scripts/technologies.js',
      autorestart: false,
    },
  ],
}

$ pm2 启动 Ecosystem.config.js

If you are using pm2, you can use it with autorestart set to false:

$ pm2 ecosystem

This will generate a sample ecosystem.config.js:

module.exports = {
  apps: [
    {
      script: './scripts/companies.js',
      autorestart: false,
    },
    {
      script: './scripts/domains.js',
      autorestart: false,
    },
    {
      script: './scripts/technologies.js',
      autorestart: false,
    },
  ],
}

$ pm2 start ecosystem.config.js

临风闻羌笛 2024-10-05 23:54:18

我在 RHEL 8 AWS EC2 实例上使用 @mikemaccana 接受的答案时收到以下错误:(code =exited, status=216/GROUP)

这是由于使用的用户/组设置为:“nobody”。

经过谷歌搜索,似乎使用用户/组作为“nobody”/“nogroup”对于守护进程来说是不好的做法,正如所回答的此处位于 unix 堆栈交换上。

将用户/组设置为我的实际用户和组后,效果非常好。

您可以输入 whomaigroups 以查看解决此问题的可用选项。

我的带有 mongodb 的完整堆栈节点应用程序的服务文件:

[Unit]
Description=myapp
After=mongod.service

[Service]
ExecStart=/home/myusername/apps/myapp/root/build/server/index.js
Restart=always
RestartSec=30
User=myusername
Group=myusername
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/home/myusername/apps/myapp

[Install]
WantedBy=multi-user.target

I received the following error when using @mikemaccana's accepted answer on a RHEL 8 AWS EC2 instance: (code=exited, status=216/GROUP)

It was due to using the user/group set to: 'nobody'.

Upon googling, it seems that using user/group as 'nobody'/'nogroup' is bad practice for daemons as answered here on the unix stack exchange.

It worked great after I set user/group to my actual user and group.

You can enter whomai and groups to see your available options to fix this.

My service file for a full stack node app with mongodb:

[Unit]
Description=myapp
After=mongod.service

[Service]
ExecStart=/home/myusername/apps/myapp/root/build/server/index.js
Restart=always
RestartSec=30
User=myusername
Group=myusername
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/home/myusername/apps/myapp

[Install]
WantedBy=multi-user.target
独﹏钓一江月 2024-10-05 23:54:18

您可以在 unix 中使用 & 在后台运行节点应用程序。确保您的节点应用程序不应该有任何交互模式,例如从输入接收数据。

node src/index.js -p 1234 --another-options &

You can run your node app in background with & in unix. Make sure your node app should not have any interactive mode like receive data from input.

node src/index.js -p 1234 --another-options &
清音悠歌 2024-10-05 23:54:18

这个答案已经很晚了,但我发现最好的解决方案是编写一个同时使用 screen -dmSnohup 命令的 shell 脚本。

screen -dmS newScreenName nohup node myserver.js >> logfile.log

我还添加了 >> logfile 位位于末尾,这样我就可以轻松保存节点 console.log() 语句。

为什么我使用 shell 脚本?我还添加了一个 if 语句来检查 node myserver.js 进程是否已经在运行。

这样我就能够创建一个命令行选项,既可以让我保持服务器运行,也可以在我进行更改时重新启动它,这对开发非常有帮助。

This answer is quite late to the party, but I found that the best solution was to write a shell script that used the both the screen -dmS and nohup commands.

screen -dmS newScreenName nohup node myserver.js >> logfile.log

I also add the >> logfile bit on the end so I can easily save the node console.log() statements.

Why did I use a shell script? Well I also added in an if statement that checked to see if the node myserver.js process was already running.

That way I was able to create a single command line option that both lets me keep the server going and also restart it when I have made changes, which is very helpful for development.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文