多年来,因为这篇文章引发了不少关注,所以我在文章的底部列出了每一个平台的最佳解决方案。 html
原始帖子 : node
我但愿个人node.js服务器在后台运行,即:当我关闭终端时,我但愿服务器继续运行。 我已经用谷歌搜索并提出了本教程 ,可是它不能按预期工做。 所以,我没有使用该守护程序脚本,而是觉得我只使用了输出重定向( 2>&1 >> file
部分),但这也不会退出-我在终端中出现空白行,就像它在等待输出/错误同样。 linux
我也尝试过将流程置于后台,可是一旦关闭终端,该流程也会被终止。 git
那么,当我关闭本地计算机时如何使它运行? github
最佳解决方案 : web
对于这个聚会来讲,这个答案很晚了,可是我发现最好的解决方案是编写同时使用screen -dmS
和nohup
命令的shell脚本。 shell
screen -dmS newScreenName nohup node myserver.js >> logfile.log
我还将在末尾添加>> logfile
位,以即可以轻松保存节点console.log()
语句。 npm
为何要使用Shell脚本? 好吧,我还添加了一条if语句,用于检查node myserver.js
进程是否已在运行。 windows
这样,我可以建立一个命令行选项,该选项既可使服务器保持运行状态,又能够在进行更改后从新启动服务器,这对开发很是有帮助。 服务器
我只是在使用守护程序 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 });
最近,我还使用TJ Holowaychuk的 mon(1)来启动和管理简单的节点应用程序。
Node.js做为WINDOWS XP中的后台服务
安装:
建立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/");
打开命令控制台并键入如下内容(仅在安装了Resource Kit的状况下才为setx)
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
一个不错的批处理好事是建立c:\\ node \\ ServiceMe.cmd
@echo off nssm.exe install node-%~n1 "C:\\Program Files\\nodejs\\node.exe" %~s1 net start node-%~n1 pause
服务管理:
2016更新: node-windows / mac / linux系列在全部操做系统上使用通用的API,所以绝对是一个相关的解决方案。 然而; node-linux生成systemv初始化文件。 随着systemd的持续普及,在Linux上其实是一个更好的选择。 若是有人想向node-linux添加systemd支持,则PR表示欢迎:-)
原始线程:
如今这是一个至关老的线程,可是节点窗口提供了另外一种在Windows上建立后台服务的方法。 它大体基于nssm
概念,即在节点脚本周围使用exe
包装器。 然而; 它改用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();
该模块支持诸如限制从新启动的上限(这样,错误的脚本不会使您的服务器瘫痪)以及从新启动之间的时间间隔愈来愈长。
因为节点Windows服务的运行方式与其余服务同样,所以可使用您已经使用的任何软件来管理/监视该服务。
最后,没有make
依赖项。 换句话说,一个简单的npm install -g node-windows
就能够了。 您不须要安装Visual Studio,.NET或node-gyp魔术。 此外,它还得到了MIT和BSD的许可。
我彻底是本模块的做者。 它旨在减轻OP所遭受的确切痛苦,但与操做系统已提供的功能之间的紧密集成。 我但愿未来有一样问题的观众也能从中受益。
为了完善建议的各类选项,这里还有一个:GNU / Linux中的daemon
命令,您能够在这里阅读: http : //libslack.org/daemon/manpages/daemon.1.html 。 (若是在上面的评论之一中已经提到过,则表示歉意)。