1. 程式人生 > >Ubuntu服務器運行js,讓js一直運行下去

Ubuntu服務器運行js,讓js一直運行下去

style err 終端 版本 .org per 當前 兩種 star

ssh連接Ubuntu,node xxx.js運行js,一關閉ssh連接,js服務就關閉了。百度了一下發現了一個大神的回答這裏copy了一個大神的回答,來記錄一下

讓 Ghost 一直運行

前面提到的啟動 Ghost 使用 npm start 命令。這是一個在開發模式下啟動和測試的不錯的選擇,但是通過這種命令行啟動的方式有個缺點,即當你關閉終端窗口或者從 SSH 斷開連接時,Ghost 就停止了。為了防止 Ghost 停止工作,有兩種方式解決這個問題。

Forever (https://npmjs.org/package/forever)
你可以使用 forever 以後臺任務運行 Ghost 。forever 將會按照 Ghost 的配置,當進程 crash 後重啟 Ghost。

通過 npm install forever 
-g 安裝 forever 為了讓 forever 從 Ghost 安裝目錄運行,輸入 NODE_ENV=production forever start index.js 通過 forever stop index.js 停止 Ghost 通過 forever list 檢查 Ghost 當前是否正在運行 Supervisor (http://supervisord.org/) 流行的 Linux 發行版——例如 Fedora, Debian 和 Ubuntu,都包含一個 Supervisor 包:一個進程控制系統,允許在啟動的時候無需初始化腳本就能運行 Ghost。不像初始化腳本一樣,Supervisor 可以移植到不同的發行版和版本。 根據不同的 Linux 發行版 安裝 Supervisor 。如下所示: Debian
/Ubuntu: apt-get install supervisor Fedora: yum install supervisor 其他大多數發行版: easy_install supervisor 通過 service supervisor start 確保 Supervisor 運行 為 Ghost 創建一個啟動腳本。通常為 /etc/supervisor/conf.d/ghost.conf ,例如: [program:ghost] command = node /path/to/ghost/index.js directory = /path/to/ghost user = ghost autostart
= true autorestart = true stdout_logfile = /var/log/supervisor/ghost.log stderr_logfile = /var/log/supervisor/ghost_err.log environment = NODE_ENV="production" 使用 Supervisor 啟動 Ghost:supervisorctl start ghost 停止 Ghost: supervisorctl stop ghost 詳細內容請參閱 Supervisor 文檔。

摘抄自
http://docs.ghostchina.com/zh/installation/deploy/

Ubuntu服務器運行js,讓js一直運行下去