CommonGarden / Grow-IoT

Software packages for smart growing environments.
http://commongarden.org/
Other
25 stars 6 forks source link

Document how to run a script reliably on boot #397

Closed JakeHartnell closed 6 years ago

JakeHartnell commented 6 years ago
catbarph commented 6 years ago

This is basically what I was trying before: $ crontab -u pi -e add line: @reboot /usr/local/bin/forever start /your/path/to/your/app.js or maybe: @reboot /usr/local/bin/forever start -c /usr/local/bin/node /your/path/to/your/app.js This info from: https://stackoverflow.com/questions/13385029/automatically-start-forever-node-on-system-restart

So I had put together: @reboot /usr/bin/sudo -u pi -H /usr/bin/forever start /home/pi/Grow.js/examples/rasp-pi/bioreactor.js -OR- @reboot /usr/bin/sudo -u pi -H /usr/bin/forever start -c /usr/local/bin/node /home/pi/Grow.js/examples/rasp-pi/bioreactor.js

catbarph commented 6 years ago

Those first parts probably aren't necessary, but I couldn't get anything to work before. Will try again soon.

catbarph commented 6 years ago

Hmmm... "it appears /home is not mounted yet, so this won't work if your code lives in /home. – chovy Dec 24 '13 at 5:59"

(from https://stackoverflow.com/questions/13385029/automatically-start-forever-node-on-system-restart)

Maybe just copy driver file?

JakeHartnell commented 6 years ago

"it appears /home is not mounted yet, so this won't work if your code lives in /home. – chovy Dec 24 '13 at 5:59"

To solve this we can install Grow.js in a directory that is not in the home folder. It is currently in the home folder I believe...

JakeHartnell commented 6 years ago

See the Grow-Hub readme:

Run on boot

Make a new text file in /etc/init.d/:

sudo nano /etc/init.d/grow

Paste in the following (note this assumes you installed Grow-Hub in the root of the home folder for a user named pi):

#!/bin/bash
#/etc/init.d/grow
export PATH=$PATH:/usr/local/bin
export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules

case "$1" in
start)
exec sudo forever --sourceDir=/home/pi/Grow-Hub/driver -p /home/pi/Grow-Hub/dri$
;;
stop)
exec sudo forever stop --sourceDir=/home/pi/Grow-Hub/driver Grow-Hub.js
;;
*)
echo "Usage: /etc/init.d/grow {start|stop}"
exit 1
;;
esac
exit 0

Make it executable with the following command:

chmod 755 /etc/init.d/grow 

Feel free to test it:

sh /etc/init.d/grow start/stop

If all goes well, make it bootable:

sudo update-rc.d grow defaults

To remove it from boot:

sudo update-rc.d -f myService remove