首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >系统重启时自动永远启动(节点)

系统重启时自动永远启动(节点)
EN

Stack Overflow用户
提问于 2012-11-15 02:31:41
回答 9查看 149.8K关注 0票数 198

我使用node的forever模块来保持我的node服务器运行。然而,当系统重新启动时,永久将终止。有没有什么方法可以在系统重新启动时自动启动节点服务器(永远)?

EN

回答 9

Stack Overflow用户

发布于 2014-08-21 18:16:53

您可以使用forever service来完成此操作。

npm install -g forever-service
forever-service install test

这将通过永远将当前目录中的app.js作为服务提供。每次系统重启时,该服务都会自动重启。此外,当停止时,它将尝试优雅地停止。此脚本还提供logrotate脚本。

Github网址:https://github.com/zapty/forever-service

注:我是永久服务的作者。

票数 125
EN

Stack Overflow用户

发布于 2016-11-03 19:23:04

已从附加的question复制答案。

您可以使用PM2,它是用于Node.js应用程序的生产流程管理器,具有内置的负载均衡器。

安装PM2

$ npm install pm2 -g

启动应用程序

$ pm2 start app.js

如果你使用express,那么你可以像这样启动你的应用程序

pm2 start ./bin/www --name="app"

列出所有正在运行的进程:

$ pm2 list

它将列出所有进程。然后,您可以通过以下命令使用应用程序的ID或名称来停止/重新启动您的服务。

$ pm2 stop all                  
$ pm2 stop 0                    
$ pm2 restart all               

显示日志的步骤

$ pm2 logs ['all'|app_name|app_id]
票数 9
EN

Stack Overflow用户

发布于 2012-11-15 03:22:22

您需要在/etc/init.d文件夹中为此创建一个shell脚本。如果你从来没有做过这件事,那就有点复杂了,但是网上有很多关于init.d脚本的信息。

下面是我为永远运行CoffeeScript站点而创建的脚本示例:

#!/bin/bash
#
# initd-example      Node init.d 
#
# chkconfig: 345 
# description: Script to start a coffee script application through forever
# processname: forever/coffeescript/node
# pidfile: /var/run/forever-initd-hectorcorrea.pid 
# logfile: /var/run/forever-initd-hectorcorrea.log
#
# Based on a script posted by https://gist.github.com/jinze at https://gist.github.com/3748766
#


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


pidFile=/var/run/forever-initd-hectorcorrea.pid 
logFile=/var/run/forever-initd-hectorcorrea.log 

sourceDir=/home/hectorlinux/website
coffeeFile=app.coffee
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
    NODE_ENV=production PORT=80 forever start --pidFile $pidFile -l $logFile -a -d --sourceDir $sourceDir/ -c coffee $coffeeFile

    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

因为init.d脚本是以root用户身份运行的,所以我必须确保文件夹和路径已显式设置或可供root用户使用。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13385029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档