如何在系统启动时启动 Node.js 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21542304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:47:54  来源:igfitidea点击:

How to start a Node.js app on system boot?

node.jsraspbianinit.d

提问by user2650875

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:

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

#! /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 "" 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

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.

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

采纳答案by Kevin Reilly

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

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

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

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

#! /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 "" 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

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

如果您愿意sudo node,可以将 PATH 添加到Defaults secure_pathusing sudo visudo

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

此外,我建议使用诸如永远这样的东西来让你的进程在崩溃后继续运行,而不是什么。

回答by Mohit Athwani

I solved this problem by first checking where node.js was installed on RaspberryPi:

我通过首先检查 node.js 在 RaspberryPi 上的安装位置解决了这个问题:

which node

This gave me :

这给了我:

/usr/local/bin/node

Open crontab config:

打开 crontab 配置:

sudo crontab -e

Then in my crontab :

然后在我的 crontab 中:

@reboot sudo /usr/local/bin/node <complete path to your .js app> &

Save, reboot, and problem solved !

保存,重启,问题解决!

回答by Adeel C

Mohit is right, but just for clarification, you can use readlinkto find the full path for your Node.js app as it will be needed later to add as a cron job.

Mohit 是对的,但为了澄清起见,您可以使用readlink来查找 Node.js 应用程序的完整路径,因为稍后需要将其添加为 cron 作业。

readlink -f <<name of file >>

For instance readlink -f HAP-NodeJS/Core.jsresults in /home/pi/HAP-NodeJS/Core.js

例如readlink -f HAP-NodeJS/Core.js导致/home/pi/HAP-NodeJS/Core.js

You can also use which nodeto find the full path where node.js is installed

您还可以使用which node查找安装 node.js 的完整路径

Next, create a new cron job using sudo crontab -eand add the following code at the very end:

接下来,使用创建一个新的 cron 作业sudo crontab -e并在最后添加以下代码:

@reboot sudo /usr/local/bin/node <<.js application path>> &

for instance, my code looks like this.

例如,我的代码看起来像这样。

@reboot sudo /usr/local/bin/node /home/pi/HAP-NodeJS/Core.js &

Upon reboot (or start up) , your Node.js should run. Hope this clears things.

重新启动(或启动)后,您的 Node.js 应该会运行。希望这能解决问题。