【问题标题】:To run node.js in system boot在系统启动时运行 node.js
【发布时间】:2014-11-02 00:03:46
【问题描述】:

我使用 node.js 编写了一个 API。我已经将它部署在生产环境中。现在我使用 forever.js 运行程序(server.js),它工作正常

但我希望我的节点在系统自行启动时运行。

我尝试在\etc\init api.conf 中创建一个包含以下内容的文件。

start on startup    
exec forever start /home/testuser/server.js

但是当我重新启动系统时,上面没有运行。请帮我解决这个问题。

提前致谢。

编辑:

最后我尝试了这个:

# Source function library.
. /lib/lsb/init-functions


NODE_ENV="production"
PORT="2100"
APP_DIR="/home/testuser/API/"
NODE_APP="server.js"
CONFIG_DIR="$APP_DIR"
PID_DIR="$APP_DIR/pid"
PID_FILE="$PID_DIR/server.pid"
LOG_DIR="/home/testuser/APIlogs/"
LOG_FILE="$LOG_DIR/project-debug.log"
NODE_EXEC=$(which node)

pidFile="$PID_DIR/server.pid" 
logFile="$LOG_DIR/project-debug.log" 

sourceDir=/home/testuser/API
coffeeFile=server.js
scriptId=$sourceDir/$coffeeFile


start() {
    echo "Starting $scriptId"

    # This is found in the library referenced at the top of the script
    start_daemon

    # Start our CoffeeScript app through forever
    # Notice that we change the PATH because on reboot
    # the PATH does not include the path to node.
    # Launching forever or coffee with a full path
    # does not work unless we set the PATH.
    cd $sourceDir
    PATH=/usr/local/bin:$PATH
    forever start /home/testuser/API/server.js

    RETVAL=$?
}

restart() {
    echo -n "Restarting $scriptId"
    /usr/local/bin/forever restart $scriptId
    RETVAL=$?
}

stop() {
    echo -n "Shutting down $scriptId"
    /usr/local/bin/forever stop $scriptId
    RETVAL=$?
}

status() {
    echo -n "Status $scriptId"
    /usr/local/bin/forever list
    RETVAL=$?
}


case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        restart
        ;;
    *)
        echo "Usage:  {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL

当我尝试以下命令时:

/etc/init.d/API start 工作正常。当我重新启动系统时,它说“NO forever process”正在运行。

【问题讨论】:

    标签: node.js ubuntu


    【解决方案1】:

    你应该看看Upstart。人use it with Node。 Forever 仅用于测试。

    【讨论】:

      【解决方案2】:

      我建议切换到pm2

      $ npm install pm2 -g
      $ pm2 start /home/testuser/server.js
      $ pm2 list
      

      PM2 可以生成和配置启动脚本,以在每次服务器重新启动时保持 PM2 和您的进程处于活动状态。

      $ pm2 startup ubuntu
      

      要保存进程列表,只需执行以下操作:

      $ pm2 save
      

      您可以查看here,了解有关在 Ubuntu 服务器上设置 pm2 以进行生产的更多信息。

      【讨论】:

        猜你喜欢
        • 2013-01-26
        • 2014-02-27
        • 2013-07-21
        • 1970-01-01
        • 2017-09-26
        • 1970-01-01
        • 2020-07-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多