Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  149] [ 3]  / answers: 1 / hits: 6175  / 9 Years ago, mon, june 29, 2015, 12:00:00

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 command 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 command 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 command 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?


More From » linux

 Answers
2

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 command 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'

[#36022] Sunday, June 28, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryantc

Total Points: 455
Total Questions: 96
Total Answers: 110

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
bryantc questions
Fri, Aug 13, 21, 00:00, 3 Years ago
Tue, Mar 30, 21, 00:00, 3 Years ago
Fri, Jun 5, 20, 00:00, 4 Years ago
Wed, May 27, 20, 00:00, 4 Years ago
;