【问题标题】:How to start nodejs application automatically on openwrt - Arduino Yun -如何在 openwrt 上自动启动 nodejs 应用程序 - Arduino Yun -
【发布时间】:2016-06-30 14:21:37
【问题描述】:

我试图让 nodejs 应用程序在系统启动时自动启动。基本上我只需要运行命令node /dir/app。 我在 Arduino Yun 上使用 openwrt。并尝试了几件事。

在 openwrt 网站上,它说我可以做到这一点。 https://wiki.openwrt.org/inbox/procd-init-scripts

#!/bin/sh /etc/rc.common
USE_PROCD=1
start_service() {
  procd_open_instance
  procd_set_param command node ///www/www-blink.js
  procd_close_instance
}

我也尝试将目录更改为/www/www-blink.js 而不是///

但是我不确定我做错了什么,因为当我尝试使用 /etc/init.d/node-app start 运行它时没有任何反应,我显然写错了代码,但我不确定它应该是什么样子。

我尝试过的另一件事是节点模块foreverforever-service。 我使用npm install -g forever 和永久服务将它们下载到我的计算机上。我将它们转移到我的 arduino yun 上的 usr/lib/node_modules。但是,当我尝试使用 and forever(-service) 命令时,它会说

-ash: forever: not found

我尝试了其他一些方法,但没有任何效果。任何帮助将不胜感激。

-- 我还需要能够使用 npm start 而不是 node app 启动我的 express 脚本,但我想第一件事就是让它工作。

【问题讨论】:

标签: linux node.js express arduino openwrt


【解决方案1】:

您可以将启动命令(node /dir/app &)放在 /etc/rc.local 脚本中。这将在系统启动时自动启动您的 nodejs 应用程序。

【讨论】:

    【解决方案2】:

    OpenWRT procd 有一个“respawn”参数,它将重新启动退出或崩溃的服务。

    # respawn automatically if something died, be careful if you have an 
    # alternative process supervisor if process dies sooner than respawn_threshold,
    # it is considered crashed and after 5 retries the service is stopped
    
    procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
    

    所以,你冷,只需添加: procd_set_param 重生 60 5 5

    或类似的东西到你的 OpenWRT procd initscript。这个 60 5 5 表示它会在两次重生之间等待 5s(中间参数),如果它在 60s(第一个参数)内重生超过 5 次(最后一个参数),它将禁用服务(检测到“重启循环”)。

    有关更多信息,请参阅此页面: https://openwrt.org/docs/guide-developer/procd-init-scripts

    【讨论】:

      【解决方案3】:

      您需要像Linux Service 一样执行您的node 应用程序。

      Upstart 非常适合这项任务

      Upstart 是 /sbin/init 守护进程的基于事件的替代品,它在启动期间处理任务和服务的启动,在关机期间停止它们并在系统运行时监督它们。

      如果您有这样的应用(例如):

      // app.js
      var express = require('express')  
      var app = express()  
      var port = process.env.PORT
      
      app.get('/', function(req, res) {  
        res.send('Hello world!')
      })
      
      app.listen(port)
      

      像这样使用package.json

      {
        "name": "my-awesome-app",
        "version": "1.0.0",
        "dependencies": {
           "express": "^4.13.3"
        },
        "scripts": {
           "start": "node app.js"
        }
      }
      

      我们使用以下代码创建一个名为 myAwesomeApp.conf 的新贵配置文件:

      start on runlevel [2345]  
      stop on runlevel [!2345]
      
      respawn  
      respawn limit 10 5
      
      setuid ubuntu  
      chdir /opt/myAwesomeApp.conf
      
      env PORT=3000
      
      exec npm start 
      

      最后,将您的应用程序(app.jspackage.json)放入 /opt/myAwesomeApp.conf 并复制配置文件 myAwesomeApp.conf 到 /etc/init/

      这就是全部,现在您只需运行service myAwesomeApp start 即可将您的节点应用程序作为服务运行

      【讨论】:

      • 如何将 upstart 安装到 arduino yun 上?
      • Upstart 已在很大程度上被 systemd 取代。
      【解决方案4】:

      我以前从未使用过 procd,但它可能需要到 node 的完整路径(例如,/usr/bin/node)。假设您要运行的文件是/www/www-blink.js,您需要使该行类似于procd_set_param command /usr/bin/node /www/www-blink.js。您可以通过运行which nodetype -a node 来定位节点。

      【讨论】:

      • 试过这个。但是,当我使用top 时,它仍然什么也没说或没有显示任何进程。我使用了/etc/init.d/node-app start,还尝试了/etc/init.d/node-app enableSTART=1
      猜你喜欢
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多