每天进步一点点:让node.js 应用一直运行的方法
cn·@oflyhigh·
0.000 HBD每天进步一点点:让node.js 应用一直运行的方法
这两天和一个node.js应用较劲,其实我这玩意我的外行中外行,七窍通了六窍——一窍不通,不过慢慢学呗。  (图源 :[pixabay](https://pixabay.com/)) 昨天好不容易把程序运行起来了,浏览器也可以访问了,大松了一口气,可是早晨起来再一试,怎么访问不了啦?再一想,这也合理,我的ssh终端窗口都死掉了,程序当然一起死掉啦。 但是我又不能一直开着终端窗口啊,那如何才能让node.js程序一直运行呢?平时遇到这样的情况,我常用的方法有`nohup`以及`screen`等,想必应该也好用,让我来试试。 # ~~nohup~~ `nohup` 使用起来很简单了,只需在执行的指令之前加上`nohup`即可。 比如待执行的命令为:`node app.js` 那么使用`nohup`来运行的指令如下: >`nohup node app.js` 执行后会提示: >nohup: ignoring input and appending output to 'nohup.out' 执行后试了一下浏览器访问,一切正常。关掉ssh窗口,再试浏览器访问,擦,访问不到了,***说明使用nohup是不可行的。*** (网上有人用nohup执行node.js程序没任何问题,所以不成功的原因可能和我运行的程序有关,懒得深究了) # screen 既然nohup失败了,我只好寄希望于screen了。 screen使用起来超级简单,先用如下命令开一个虚拟窗口 >`screen` 然后正常执行命令即可 >`node app.js` 用浏览器测试访问,一起正常,关闭SSH终端窗口再测试访问,一切正常,说明使用`screen`让node.js一直运行是可行的。 # forever 网上搜索较为常用的方式是使用forever,使用之前需要先安装forever,一般是选择全局安装 >`sudo npm install -g forever` 因为我不打算全局使用,所以只安装到项目目录: >`npm install forever` 然后使用如下指令运行: >`node_modules/forever/bin/forever start app.js` 然而关掉终端窗口后,浏览器浏览器依旧可以正常访问,看来这个是行得通的。 # PM2 另外一种更高级的方式据说是PM2,全局安装的方式如下: >`sudo npm install pm2 -g` 我还是安装在项目目录内: >`npm install pm2` 启动应用: >`node_modules/pm2/bin/pm2 start api.js` 关闭SSH终端窗口测试浏览器访问,一切正常。据说这个PM2还支持很多高级功能,比如说聚簇模式(Cluster Mode: Node.js Load Balancing & Zero Downtime Reload),这个暂时就不测试啦,感兴趣的同学自己去文末链接里探索吧。 # 相关链接 * https://github.com/foreversd/forever * https://github.com/Unitech/pm2 ---- <center><strong>Vote For Me As Witness</strong> https://steemit.com/~witnesses type in **`oflyhigh`** and click ***`VOTE`*** [](https://steemit.com/~witnesses) [Vote @oflyhigh via Steemconnect](https://steemconnect.com/sign/account-witness-vote?witness=oflyhigh&approve=1) <strong>Thank you!</strong></center>
👍 liangfengyouren, bambugrove, astros, money-way, blues-wclouds, cryptouno, eval, ms8988, auntigormint, ikonik, lemminon, sultan-aceh, accelerator, gamezine, holmesian, map10k, trailmakers, rea, jamzmie, technologynepal, julian2013, discernente, evelaurie, yalehazlitt, eltonwright, zorapolly, sskiller, archisteem, yettawild, bobarchibald, annasandy, hamsa.quality, culam92, wylo, davidke20, elizacheng, namchau, joeliew, kimzwarch, phatima, bukiland, exec, chinadaily, devilwsy, laoyao, lalala, wongshiying, blackbunny, yulan, oflyhigh, xiaokongcom, deanliu, janiceting, ethansteem, yyyy, lingfei, midnightoil, helene, xiaohui, alexis555, newhope, thecryptodrive, steemprotect, canbethisone, smartcash, busy.pay, ratticus, aabb, cryptohustler, mrliga, huatanzhang, windowglass, abetterworld, raili, weavingwords, heyeshuang, sanzo, winniex, chenlocus, fastiduos, rebecca80, silentlucidity53, sweethoney, khalilad, stakuza, bearpaw, razor80, cryptoknight27, alijewel, kingofdew, freedom-fighter, chaerin, chann, coindzs, ethanlee, raku, lkvictor2005, speeding, cnbuddy, minloulou, cryptocurrencyhk, tumutanzi, fyrstikken, blockchainbilly, jkkim, hongman96, jianghao, teachblogger, dgorbunov, maiyude, fredo77200, agoha, resteeming, asterix87, davaowhenyo, allenshayzar, historylover, austinsandersco, walkinharmony, ravenousappetite, nitro.live, fanso, frankintaiwan, ebejammin, that1consultant, jiangchen, lancy, vfxness, xiaoshancun, sasaadrian, nuagnorab, partiko, jesse5th, goodcontentbot, celine-robichaud, victory622, jincheng, amao1018, ran.koree, anonsurf, slowwalker, ace108, everrich, idx, abit, mia-cc, adm, china-joke, bxt, matthewtonyit, timourick, windenchanter,