I know this has been as been asked before. I followed lots of tuts how to do this and I want to do it with cron.
First problem is sudo vs regular user. I write in nodeJS, while I managed to have both users on the same up-to-date node version I'd prefer not to use sudo.
This is what I tried:
which node
outputs/home/pi/.nvm/versions/node/v7.5.0/bin/node
crontab -e
and use nano- I add
@reboot /home/pi/.nvm/versions/node/v7.5.0/bin/node /home/pi/projects/pi-bootup/server.js
, exit and save. - I insert
/home/pi/.nvm/versions/node/v7.5.0/bin/node /home/pi/projects/pi-bootup/server.js
to test if the command works (it does). - I check with
ps -e|grep node
for running node tasks (yup, they're there) sudo reboot
.- after reboot, I check for running node tasks with
ps -e|grep node
. None started.
What am I doing wrong?
EDIT:
I also added @reboot echo "hi" > /home/pi/reboot.txt 2>&1
and the file is created after reboot. So the reboot jobs run, just the node task isn't running... or am I verifying this the wrong way?
Thanks in advance for any help. I'm new to Raspberry and Linux, (kinda new to node too).
2 Answers 2
Just like @tobyd said: it was better to not use cron. No idea why. I made it work using this tutorial: http://weworkweplay.com/play/raspberry-pi-nodejs/
-
For a marginally more flexible solution you could use either supervisor (supervisord.org/running.html) or systemd (freedesktop.org/software/systemd/man/systemd.service.html) to handle automatic restarts for you. rc.local IRC doesn't handle this scenario and node apps can be problematic on failure. Glad its sorted though.user29019– user290192017年02月03日 11:04:43 +00:00Commented Feb 3, 2017 at 11:04
-
Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year.Ingo– Ingo2019年12月11日 20:09:48 +00:00Commented Dec 11, 2019 at 20:09
If you can successfully run your node
command from your interactive shell (default bash
on RPi OS), then you can also run it in cron
under @reboot
. When a program runs from the command line/interactive shell, but fails to run under cron
with the @reboot
facility, this is usually because crond
has started, but a service required by the program has not yet been started by init
- which for RPi OS is handled by systemd
.
Typically, the simple solution is to instruct cron
to sleep
for a short time before trying to execute the program:
@reboot /bin/sleep 30; /home/pi/.nvm/versions/node/v7.5.0/bin/node /home/pi/projects/pi-bootup/server.js
At boot time, cron
will run sleep
for 30 seconds, and then will run node
. Nothing magic about 30 seconds - feel free to try other (larger or smaller) values as required.
&
to the end of your script definition to background it. I'm not entrely sure cron would be happy running a foreground task on boot? That would be my only suggestion. Theecho hi
bit would be fast and not cause problems but you might find the OS kills off the node process early on for causing a delay.