多年來,因爲這篇文章引發了不少關注,所以我在文章的底部列出了每一個平臺的最佳解決方案。 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 。 (若是在上面的評論之一中已經提到過,則表示歉意)。