鍍金池/ 問答/Linux/ 在Linux上同時(shí)執(zhí)行多個(gè)npm自定義的命令,兩個(gè)命令都是啟動(dòng)Node站點(diǎn)。并且

在Linux上同時(shí)執(zhí)行多個(gè)npm自定義的命令,兩個(gè)命令都是啟動(dòng)Node站點(diǎn)。并且在后臺(tái)運(yùn)行

"start-client": "cross-env NODE_ENV=production node ./server/index.js",
"start-api": "cross-env NODE_ENV=production node ./server/api/index.js",
"start-pro": "concurrently \"npm run start-client\"  \"npm run start-api\""

以上的npm run start-pro在linux上直接運(yùn)行是可以跑起來(lái)的,但是關(guān)掉當(dāng)前會(huì)話之后服務(wù)就存在了也就關(guān)閉了。

當(dāng)我執(zhí)行npm run start-pro &這個(gè)命令的時(shí)候,也正常的跑起來(lái)了,當(dāng)我關(guān)閉會(huì)話之后,站點(diǎn)出現(xiàn)了503的錯(cuò)誤,使用lsof -i:8080lsof -i:8686查看端口是否存在,發(fā)現(xiàn)8080的端口不存在,但是8686的后端端口還存在。然后就放棄了這種操作。

當(dāng)我執(zhí)行nohup npm run start-pro &這個(gè)命令的時(shí)候,在項(xiàng)目的根目錄出現(xiàn)了nohup.output的文件,里面記錄了錯(cuò)誤:


> p2@0.1.0 start-pro /website/pgyer
> concurrently "npm run start-client"  "npm run start-api"

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: EBADF: bad file descriptor, read
    at Error (native)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! p2@0.1.0 start-pro: `concurrently "npm run start-client"  "npm run start-api"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the p2@0.1.0 start-pro script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-07-18T00_30_14_968Z-debug.log

再次想辦法,使用先執(zhí)行nohup npm run start-api &提示成功!在執(zhí)行nobup npm run start-client &也執(zhí)行成功!

本人比較強(qiáng)迫癥,在這里想問一下,如何能一條命令一下執(zhí)行npm run start-apinpm run start-client有什么方法嗎?
回答
編輯回答
來(lái)守候

@熊丸子

我試了一下,添加了

 "start-all": "npm run start-client && npm run start-api"

執(zhí)行了nohup npm run start-all &這個(gè)命令,查看nohup,out發(fā)現(xiàn)只能執(zhí)行start-client的命令

2017年2月16日 09:18
編輯回答
陪妳哭

對(duì)nodejs不熟悉,無(wú)法從nodejs的應(yīng)用層面提供幫助,不過沒有好的辦法的話可以試試下面兩種方案

  1. 寫個(gè)shell腳本startup.sh,通過腳本來(lái)啟動(dòng)這兩個(gè)服務(wù)

     #!/usr/bin/env bash
    
     nohup npm run start-api &
     nohup npm run start-client &
    

    這樣執(zhí)行 ./startup.sh 就一個(gè)命令啟動(dòng)兩個(gè)服務(wù)了

  2. 推薦使用的方式是PM2來(lái)管理node進(jìn)程,PM2負(fù)責(zé)了node進(jìn)程的啟動(dòng),停止以及運(yùn)行時(shí)監(jiān)控等。
2018年4月22日 04:56
編輯回答
凝雅
//package.json

"script":{
    "start-client": "cross-env NODE_ENV=production node ./server/index.js",
    "start-api": "cross-env NODE_ENV=production node ./server/api/index.js",
    "start-all": "npm run start-client && npm run start-api"
}
npm run start-all
2017年2月8日 02:57