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 bothsudo
andshutdown
. – 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
1 Answer
Reset to default 7When 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条)