How to start NodeJS app from bash with file permissions

I have built a NodeJS application that is running on a Raspberry Pi.

The app runs a child_process: raspistill, which captures an image using the Raspberry Pi camera module and writes it to a file.

The node app then watches for changes in that file.

If I start the node app from the terminal manually, I have no problems, but if I start it from a bash script (/etc/rc.local) when the Pi starts up, it doesn't work.

I'm unsure as to exactly what is going wrong, but I guess that it is because it does not have permissions to write the file to disk because I see the red camera light turn on and then nothing.

So the question is how can I enable the app to write to disk after being started from the rc.local script?

Rather than launching your Node application with /etc/rc.local, you might want to use Cron's @reboot target instead, and launch Node within a GNU Screen session. You'd use a crontab something like this:

@reboot screen -h 5000 -d -m /path/to/nodewrapper.sh

where nodewrapper.sh sets up your environment variables and launches Node.

The advantages here include that:

  • you can see debug output from Node in Screen's history, and
  • you get to run commands using an environment (yours) that you know works.

With the debug output from Node, you may see an error that makes your actual problem obvious.

Note that TMTOWTDI. I don't claim that way this is best, just that it works for me.