如何在系统启动时启动 Node.js 应用程序? [英] How to start a Node.js app on system boot?

查看:50
本文介绍了如何在系统启动时启动 Node.js 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发运行 Raspbian 的 Raspberry Pi,运行 Node.js 应用程序,并尝试在 Pi 启动时启动它.我找到了几个例子,但我似乎无法让它工作.我当前的代码是:

I'm working on a Raspberry Pi running Raspbian running a Node.js app and trying to get it to start when the Pi boots. I found a couple of examples but I can't seem to get it working. My current code is:

#! /bin/sh
# /etc/init.d/MyApp

### BEGIN INIT INFO
# Provides:          MyApp.js
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts MyApp.js
# Description:       Start / stop MyApp.js at boot / shutdown.
### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system
case "$1" in
   start)
    echo "Starting MyApp.js"
    # run application you want to start
    node /home/pi/app/MyApp/MyApp.js
   ;;
   stop)
    echo "Stopping MyApp.js"
    # kill application you want to stop
    killall MyApp.js
    ;;
  *)
    echo "Usage: /etc/init.d/MyApp {start|stop}"
    exit 1
    ;;
esac

exit 0

我在 etc/init.d 文件夹中有这个,运行 chmod +x/etc/init.d/MyApp,我可以手动运行它,然后我运行 sudo update-rc.d MyApp defaults,重新启动,脚本永远不会运行.我查看了一些不同的示例,进行了调整,但仍然没有运气.

I have this in the etc/init.d folder, ran chmod +x /etc/init.d/MyApp, I'm able to run it manually, then I run sudo update-rc.d MyApp defaults, reboot and the script never runs. I've looked at some different examples, made adjustments and still no luck.

推荐答案

如果您使用的是预构建的 Pi 版本,例如 0.10.24,您可能遇到 PATH 问题.

If you're using a prebuilt Pi release like 0.10.24, you may be experiencing a PATH issue.

您可以在 start 命令中提供节点二进制文件的完整路径,或者确保在 /etc/init.d/MyApp 之前设置节点二进制文件的 PATH 运行.我遇到了同样的问题,并成功地尝试了两者.此外,您拥有的 stop 命令可能无法正常工作.

You can either provide the full path to the node binary as part of the start command or make sure the PATH to the node binaries are set before /etc/init.d/MyApp is ran. I had the same issue and tried both with success. Also, the stop command as you have it may not be working.

#! /bin/sh
# /etc/init.d/test

### BEGIN INIT INFO
# Provides:          test
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Carry out specific functions when asked to by the system
case "$1" in
   start)
    echo "Starting test.js"
    # run application you want to start
    #node /home/pi/test.js > /home/pi/test.log
    /home/pi/downloads/node-v0.10.24-linux-arm-pi/bin/node /home/pi/test.js >> /home/pi/test.log
   ;;
   stop)
    echo "Stopping test.js"
    # kill application you want to stop
    killall -9 node
    # Not a great approach for running
    # multiple node instances
    ;;
  *)
    echo "Usage: /etc/init.d/test {start|stop}"
    exit 1
    ;;
esac

exit 0

如果你想做 sudo node,你可以使用 sudo visudo 将 PATH 添加到 Defaults secure_path.

If you'd like to do sudo node, you can add the PATH to Defaults secure_path using sudo visudo.

此外,我建议使用类似 forever 之类的东西来让您的进程在崩溃后继续运行,而其他情况则不然.

Also, I would recommend using something like forever to keep your process running after crashes and what not.

这篇关于如何在系统启动时启动 Node.js 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆