2

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:

  1. which node outputs /home/pi/.nvm/versions/node/v7.5.0/bin/node
  2. crontab -e and use nano
  3. I add @reboot /home/pi/.nvm/versions/node/v7.5.0/bin/node /home/pi/projects/pi-bootup/server.js, exit and save.
  4. 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).
  5. I check with ps -e|grep node for running node tasks (yup, they're there)
  6. sudo reboot.
  7. 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).

asked Feb 1, 2017 at 18:23
4
  • Is using cron a firm requirement? If you just want to start a node server on boot using systemd offers a lot more flexibility in terms of restarting, which user is running the process, environment variables and so on? Commented Feb 2, 2017 at 10:50
  • @tobyd not really a requirement. It seemed easier though. I'll switch if this isn't possible using cron but right now I'd like to know why this isn't working. Commented Feb 2, 2017 at 11:23
  • you could try adding an & 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. The echo 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. Commented Feb 2, 2017 at 11:38
  • made it work with this: weworkweplay.com/play/raspberry-pi-nodejs Commented Feb 3, 2017 at 10:08

2 Answers 2

1

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/

answered Feb 3, 2017 at 10:08
2
  • 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. Commented 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. Commented Dec 11, 2019 at 20:09
1

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.

answered Nov 24, 2021 at 3:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.