javascript - How to execute shutdown command from Node.js app on Raspberry Pi - Stack Overflow

I have a headless Raspberry Pi running a simple NodeJS application.The only interface I have attached t

I have a headless Raspberry Pi running a simple NodeJS application.

The only interface I have attached to the Pi is a single push-button which starts and stops a timelapse video recording.

I know it's not good practice to cut the power to the Pi without a proper shutdown, so I want to add a shutdown mand to Node.

Using ShellJS, I can do this very simply - if the user holds down the push-button for five seconds, I can call

shell.exec('sudo shutdown -h now');

which will shutdown the Pi. This works as expected when I'm connected to the Pi via ssh and I call the node mand myself ('node app.js'). But my goal is to have my Node app running automatically on startup. I'm doing that by using '/etc/rc.local' to call the script on boot:

su pi -c 'node /path/to/app.js'

In this case the shutdown mand does not work, and I don't even know how to access the node console to see what kind of error it's throwing. Can someone point me in the right direction here?

I have a headless Raspberry Pi running a simple NodeJS application.

The only interface I have attached to the Pi is a single push-button which starts and stops a timelapse video recording.

I know it's not good practice to cut the power to the Pi without a proper shutdown, so I want to add a shutdown mand to Node.

Using ShellJS, I can do this very simply - if the user holds down the push-button for five seconds, I can call

shell.exec('sudo shutdown -h now');

which will shutdown the Pi. This works as expected when I'm connected to the Pi via ssh and I call the node mand myself ('node app.js'). But my goal is to have my Node app running automatically on startup. I'm doing that by using '/etc/rc.local' to call the script on boot:

su pi -c 'node /path/to/app.js'

In this case the shutdown mand does not work, and I don't even know how to access the node console to see what kind of error it's throwing. Can someone point me in the right direction here?

Share Improve this question asked Jun 29, 2015 at 8:03 Public TrustPublic Trust 1101 silver badge9 bronze badges 2
  • Does the pi user have sudo privs? Also, it may be a $PATH issue, try using the full paths to both sudo and shutdown. – robertklep Commented Jun 29, 2015 at 8:52
  • That works much better, thank you. But can you explain why? Since I'm learning Linux I would love to understand when & why I need to provide full paths for mands. – Public Trust Commented Jun 29, 2015 at 10:18
Add a ment  | 

1 Answer 1

Reset to default 7

When you start processes from /etc/rc.local, those processes will be started with a limited $PATH variable (the $PATH variable contains a list of directories where to find executable programs, so you don't have to start those programs using their full path; instead, just their name will suffice).

This usually doesn't contain the paths to system binaries, like shutdown, which can be found in /sbin.

Your login shell most likely does add those system paths to $PATH, which is why—when starting your Node app from the mand line—the shutdown executable works just fine.

But when the same Node app is started from /etc/rc.local, the shutdown executable can't be found in any of the directories in $PATH, and trying to execute it will result in an error.

You can solve this by either using the full path to the shutdown executable, or by augmenting the $PATH variable in /etc/rc.local:

# /etc/rc.local
export PATH=/sbin:/usr/sbin:$PATH
su pi -c 'node /path/to/app.js'

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744183583a4562098.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信