0x001
V2EX  ›  Node.js

vps 上运行 nodejs 后台运行

  •  
  •   0x001 · Jun 2, 2014 · 12195 views
    This topic created in 4548 days ago, the information mentioned may be changed or developed.
    我用ssl命令连接到digitalocean的vps, 我node app.js运行nodejs app后,如果我关闭这个ssl连接,那vps上nodejs app也会结束。

    请问什么命令让它一直运行在后台,我ssl连接断后,nodejs app也一直运行?
    Supplement 1  ·  Jun 2, 2014
    ssh远程后,screen这个命令可以新建一个窗口,可以ctrl a d离开,这是我需要的。非常不错。
    26 replies  •  2014-06-19 12:48:52 +08:00
    XXOO
    XXOO
    Jun 2, 2014
    1
    pm2
    0x001
    0x001
    OP
    Jun 2, 2014
    2
    @XXOO pm2没有这个命令呀

    不过我找到了nohup node app.js
    66CCFF
    66CCFF
    Jun 2, 2014
    3
    如果你想要崩溃重启可以npm install forever -g
    chemzqm
    chemzqm
    Jun 2, 2014
    4
    PM2功能多一些,但是使用起来也更复杂,一般forever就够了,附送一段bash脚本

    #! /bin/bash

    DIR=/home/dash/__placeholder__
    LOG_FOLDER=$DIR/log
    DATE=`date +%Y%m%d`

    mkdir -p $LOG_FOLDER

    case "$1" in
    start)
    PORT=9380 NODE_ENV=production forever start -a -l $LOG_FOLDER/__placeholder__.log -o $LOG_FOLDER/out.log -e $LOG_FOLDER/err.log $DIR/app.js
    ;;
    stop)
    forever stop $DIR/app.js
    ;;
    restart)
    $0 stop
    $0 start
    ;;
    status)
    ;;
    reload)
    ;;
    *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
    esac
    exit 0
    jakwings
    jakwings
    Jun 2, 2014
    5
    自己安装 pm2
    然后 pm2 app.js
    cevincheung
    cevincheung
    Jun 2, 2014
    6
    @0x001

    node app.js > /dev/null &
    lm902
    lm902
    Jun 2, 2014
    7
    运行 nohup ./app.js 然后直接关掉控制台
    日志会保存到nohup.out
    zoudm
    zoudm
    Jun 2, 2014
    8
    是ssh连接? ssl是啥。。

    ssh的话 screen命令也可以
    chenshaoju
    chenshaoju
    Jun 2, 2014
    9
    我的SS(node.js)是这样后台运行的:
    (node /var/shadowspdy/bin/spserver >/dev/null 2>&1 &)
    0x001
    0x001
    OP
    Jun 2, 2014
    10
    @cevincheung 这个不能看到日志
    ohsc
    ohsc
    Jun 2, 2014
    11
    pm2是个好东西
    0x001
    0x001
    OP
    Jun 2, 2014
    12
    @jakwings pm2 logs看到的日志有好多都是库输出的,我只想看到我的console.log的日志,你知道如何弄吗?
    0x001
    0x001
    OP
    Jun 2, 2014
    13
    @zoudm 对我是ssh连接,打错了。 ssh screen命令可能就是我想要的
    blueandhack
    blueandhack
    Jun 2, 2014
    14
    使用pm2部署应用
    ik0r
    ik0r
    Jun 2, 2014
    15
    我使用forever 比较习惯, pm2感觉有点复杂
    XXOO
    XXOO
    Jun 2, 2014
    16
    pm2多牛啊。还可以有监控,还可以根据cpu核心来运行。。。
    laogui
    laogui
    Jun 2, 2014
    17
    pm2比forever更好用
    jakwings
    jakwings
    Jun 2, 2014
    18
    @0x001 貌似还没有这个功能……
    不过至少可以不保存 pm2 的日志:pm2 -o /dev/null -e /dev/null
    cevincheung
    cevincheung
    Jun 2, 2014
    19
    @0x001

    command > /file 呗 。
    qianlifeng
    qianlifeng
    Jun 2, 2014
    20
    screen
    foru17
    foru17
    Jun 3, 2014
    21
    目前我是用forever
    fedoral
    fedoral
    Jun 3, 2014
    22
    forever +1
    ysjdx
    ysjdx
    Jun 3, 2014
    23
    screen + 1
    koolob
    koolob
    Jun 3, 2014
    24
    我用的nohup
    fansekey
    fansekey
    Jun 4, 2014
    25
    @XXOO pm2 `npm install -g pm2`
    solodxg
    solodxg
    Jun 19, 2014
    26
    可参见这一篇 《如何让 Ghost 一直运行》 http://solodu.com/how-to-keep-ghost-running/ 介绍了forever pm2 和Upstart
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2000 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 61ms · UTC 12:12 · PVG 20:12 · LAX 04:12 · JFK 07:12
    ♥ Do have faith in what you're doing.