Starting a NODE app from a Cron job or start-on-boot

Nov 12, 2026 0 Replies

I've got a Pi running Raspberry Pi OS Bookworm. I've been starting a NODE app (not mine - one that I've downloaded) from a cron job and using one of the start-at-boot mechanisms (I forget which!).



This has worked until did a "sudo apt update" and "sudo apt upgrade".



Now the cron job fails to start the app, even though an explicit background job works from a terminal window.



The script that is run from the crob job is



#start cd /srv/ext/NAS/tplink-energy-monitor



# At midnight, output a double-row separator; otherwise a single-row separator date | grep " 00:00" if [ $? -eq 0 ]; then echo "====================" >> /srv/ext/NAS/tplink-energy-monitor/tp.log else echo "--------------------" >> /srv/ext/NAS/tplink-energy-monitor/tp.log fi



date >> /srv/ext/NAS/tplink-energy-monitor/tp.log



echo "before 'npm start'" >> /srv/ext/NAS/tplink-energy-monitor/tp.log ps -alef | grep -e node -e npm | grep -v grep | grep -v fusermount >> /srv/ext/NAS/tplink-energy-monitor/tp.log



npm start &



echo "after 'npm start'" >> /srv/ext/NAS/tplink-energy-monitor/tp.log ps -alef | grep -e node -e npm | grep -v grep | grep -v fusermount >> /srv/ext/NAS/tplink-energy-monitor/tp.log



sleep 30



echo "after 'sleep 30'" >> /srv/ext/NAS/tplink-energy-monitor/tp.log date >> /srv/ext/NAS/tplink-energy-monitor/tp.log ps -alef | grep -e node -e npm | grep -v grep | grep -v fusermount >> /srv/ext/NAS/tplink-energy-monitor/tp.log #end



The critical lines are



cd /srv/ext/NAS/tplink-energy-monitor npm start &



The rest is logging to check that it works OK.


The script runs fine - all the logging stuff is OK. But it seems that "node" does not like running as a background job any more when invoked from a cron job as opposed to a terminal session.



The reason it's run from a cron job is that it has a habit of crashing every so often (no idea why, and writer of the app doesn't reply to emails) so I run it from cron every 5 minutes to restart it if it has stopped.


Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required