由于这篇文章在过去几年里受到了很多关注,所以我在文章的底部列出了每个平台的最佳解决方案。


原来的帖子:

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

我也尝试过把进程放在后台,但是只要我关闭终端,进程就会被杀死。

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


高级解决方案:

Systemd (Linux) Launchd (Mac) node-windows (Windows) PM2 (node . js)


当前回答

2017年6月更新: Linux解决方案:(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

其他回答

我很惊讶居然没有人提到Guvnor

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

编辑:但是,我不确定它是否在windows上工作。我只在linux上使用过。

这可能不是被接受的方式,但我在屏幕上这样做,特别是在开发过程中,因为我可以在必要时恢复它并愚弄它。

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

屏幕将分离,并幸存你注销。然后你可以用screen -r把它找回来。点击屏幕手册了解更多细节。你可以给屏幕命名等等。

更新-正如下面的一个答案所提到的,PM2有一些真正好的功能永远消失了。考虑使用它。

原来的答案

使用nohup:

nohup node server.js &

我想补充的是,公认的答案才是正确的方法。我在需要保持的实例上使用forever。我喜欢永远执行npm install -g,这样它就在节点路径中,然后永远启动server.js

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

http://github.com/pgte/fugue

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

创建业务脚本,拷贝到/etc/init/nodejs.conf目录下

Start service: sudo service nodejs Start

Stop service: sudo service nodejs Stop

服务脚本

description "DManager node.js server - Last Update: 2012-08-06"
author      "Pedro Muniz - pedro.muniz@geeklab.com.br"

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