Reputation: 110
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?
Upvotes: 3
Views: 6488
Reputation: 203304
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'
Upvotes: 7