简体   繁体   English

如何通过 /etc/rc.local 运行我的 node.js 应用程序?

[英]How to run my node.js application through /etc/rc.local?

I want to run a node application from my raspberry pi.我想从我的树莓派运行一个节点应用程序。 The application is supposed to start on boot.该应用程序应该在启动时启动。

I have included the following lines in /etc/rc.local (before exit 0) :我在 /etc/rc.local (exit 0 之前) 中包含了以下几行:

    cd /home/pi/PPBot
    node bot.js > dev/null &

I first navigate to the correct folder and then run the bot from there.我首先导航到正确的文件夹,然后从那里运行机器人。 However the node application is not running when I reboot my raspberry pi.但是,当我重新启动树莓派时,节点应用程序没有运行。 So it seems that rc.local is not executed or unable to execute the lines I provided.所以看起来 rc.local 没有执行或无法执行我提供的行。

I am looking for a solution so that the application will run at boot.我正在寻找一种解决方案,以便应用程序在启动时运行。

save this保存这个

[Unit]
Description=Node JS Script Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/node /path/to/hello_env.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

to /etc/systemd/system/ as nodescript.service sudo systemctl daemon-reload sudo systemctl start nodescript if it worked, make it startup on boot sudo systemctl enable nodescript到 /etc/systemd/system/ 作为nodescript.service sudo systemctl daemon-reload sudo systemctl start nodescript如果它工作,让它在引导时启动sudo systemctl enable nodescript

The examples I 've seen (like this one ) used我见过的例子(像这个)使用

cd /home/pi/PPBot
node bot.js < dev/null &

instead of代替

cd /home/pi/PPBot
node bot.js > dev/null &

Note the < instead of the > .注意<而不是> As you can see here , the < redirects the Standard In (stdin), while the > redirects the Standard Output (stdout).正如您在此处看到的, <重定向标准输入 (stdin),而>重定向标准输出 (stdout)。 And as described here ,正如这里所描述的,

< /dev/null is used to instantly send EOF to the program, so that it doesn't wait for input ( /dev/null , the null device, is a special file that discards all data written to it, but reports that the write operation succeeded, and provides no data to any process that reads from it, yielding EOF immediately). < /dev/null用于立即向程序发送 EOF,使其不等待输入( /dev/null ,空设备,是一个特殊的文件,它会丢弃所有写入它的数据,但报告说写操作成功,并且不向任何从中读取数据的进程提供数据,立即产生 EOF)。 & is a special type of command separator used to background the preceding process. &是一种特殊类型的命令分隔符,用于背景前面的进程。

Since you havent used < dev/null , maybe your program is stuck and waits for some input.由于您尚未使用< dev/null ,因此您的程序可能卡住并等待某些输入。 The & at the end makes your program run in the background.末尾的&使您的程序在后台运行。 Try to not include it and see if your Rasberry Pi is still booting.尽量不包括它,看看你的 Rasberry Pi 是否仍在启动。 If not, you know that your programs runs continously and blocks further booting;如果没有,您就知道您的程序会持续运行并阻止进一步启动; when you used the & sign, your program just failed in a separate process.当您使用&符号时,您的程序只是在一个单独的进程中失败了。 In this case changing the > to < should help because your programs doesn't wait for input.在这种情况下,将>更改为<应该会有所帮助,因为您的程序不会等待输入。

If the above doesn't work, update your question with the specific error message.如果上述方法不起作用,请使用特定的错误消息更新您的问题。 You can view the boot log by using the command dmesg (display message).您可以使用命令dmesg (显示消息)查看引导日志。

In this case when you cd /home/pi/PPBot you are no longer in the root directory so when you run node bot.js > dev/null & it is looking for the dev folder at /home/pi/PPBot/dev在这种情况下,当您cd /home/pi/PPBot您不再位于根目录中,因此当您运行node bot.js > dev/null &它会在/home/pi/PPBot/dev寻找dev文件夹

You will need to add a leading / to ensure it is accessing /dev您将需要添加一个前导/以确保它正在访问/dev

change改变

node bot.js > dev/null &

to

node bot.js > /dev/null &

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM