Node.js的作为后台服务 [英] Node.js as a background service

查看:140
本文介绍了Node.js的作为后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于这个帖子已经得到了很多的关注,多年来,我列出了每个平台上的解决方案在这个岗位的底部。


原帖

我希望我的Node.js服务器在后台运行,即:当我闭上终端我想我的服务器保持运行。我GOOGLE了这一点,这个<一上来href=\"http://kevin.vanzonneveld.net/techblog/article/run_nodejs_as_a_service_on_ubuntu_karmic/\">tutorial,然而,由于预期这是行不通的。因此,而不是使用守护进程的脚本,我以为我只是用输出重定向(即 2 - ;&放1 GT;&GT;文件部分),但是这也太不退出 - 我得到了我的终端一个空行,像它的等待输出/错误

我也试图把过程在后台,但只要我闭上终端的进程被杀害也是如此。

所以,我怎么能离开它运行时,我关闭了我的本地计算机?


热门解决方案


解决方案

从<一个复制我自己的答案href=\"http://stackoverflow.com/questions/4681067/how-to-run-a-node-js-application-as-its-own-process/28542093#28542093\">How运行的Node.js应用程序作为自己的进程?

2015年的答案:几乎所有的Linux发行版自带systemd,这意味着永远的monit等不再是必需的 - 你的操作系统已经处理了这些任务

请在 myapp.service 文件(与你的应用程序的名称替换的myapp',很明显):

  [单位]
说明=我的应用程序[服务]
ExecStart = /无功/网络/的myapp / app.js
重启=始终
用户=没有人
组=没人
环境= PATH =的/ usr / bin中:在/ usr / local / bin目录
环境= NODE_ENV =生产
工作目录= /无功/网络/的myapp[安装]
WantedBy = multi-user.target

你的服务文件复制到的/ etc / systemd /系统

启动该systemctl启动的myapp <​​/ code>。

启用它与开机运行systemctl启用的myapp <​​/ code>。

请参阅与 journalctl -u的myapp <​​/ code>

日志

在更多细节: 我们如何在Linux上部署节点的应用程序,2016年版

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:

解决方案

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

2015 answer: nearly every Linux distro comes with systemd, which means forever, monit, 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
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/myapp

[Install]
WantedBy=multi-user.target

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

More details at: How we deploy node apps on Linux, 2016 edition

这篇关于Node.js的作为后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆