Starting a node/npm program at boot time

Sep 19, 2022 Last reply: 3 years ago 3 Replies

I have found a node/npm program which monitors electricity usage on energy-monitoring TPLink smart switches. It is supplied with a shell script (listed below) which I am calling from /etc/rc.local



/home/pi/tplink/tplink-energy-monitor/tplink-start.sh



#!/bin/bash



cd /home/pi/tplink/tplink-energy-monitor npm start &


/etc/rc.local (the operative line is the execution of /home/pi/tplink/tplink-energy-monitor/tplink-start.sh)


#!/bin/sh -e # # rc.local # # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing.



# Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi



# start monitoring the energy usage of the freezers and other TPLink HS110 start plugs /home/pi/tplink/tplink-energy-monitor/tplink-start.sh



exit 0


rc.local has the attributes


-rwxr-xr-x 1 root root 562 Sep 19 19:49 rc.local



so it is executable by everyone. I presume it is run as root, so similar to being logged in as a non-root user but using sudo.


This appears to start node and npm, as well as the app.js



ps -alef | grep node lists


0 S root 1907 1894 0 80 0 - 473 - 19:05 pts/1 00:00:00 sh -c node ./app.js
4 S root 1908 1907 4 80 0 - 38800 - 19:05 pts/1 00:00:21 node ./app.js

and



ps -alef | grep npm lists


4 S root 1894 1 0 80 0 - 33909 - 19:05 pts/1 00:00:02 npm


However the web interface that the app presents (for displaying graphs of electricity usage) fails to locate smart plug devices.



Starting the script manually works fine:



(start a Terminal window)


sudo /home/pi/tplink/tplink-energy-monitor/tplink-start.sh &


What is the best remedy for running the script automatically at boot time? Should the rc.local entry have a "&" on the end so it runs in the background? Is it a problem with the script expecting to run in a Terminal window that continues to have stdout?


Does it work if you start it as the root user instead of using sudo? When you use sudo, environment variables such as $HOME remain set to the value for the user who runs the sudo command. If you "sudo su" and then run the command, that is the same environment where rc.local will start the script, where $HOME will be "/root".

No, because tplink-start.sh appears to background the main task itself.

Maybe, I don't know anything about Node JS. The fact that looking up "npm" finds descriptions of it as a package manager confuses me already.

On Mon, 19 Sep 2022 20:00:02 +0100, "NY" snipped-for-privacy@privacy.invalid declaimed the following:

Cron job in the /user/ account (since you claim manual start works -- unless you are using sudo to start it). I believe @reboot is an option for specifying when to run. Might need to add a short delay to ensure enough of the OS is up to support the task.

This seems like a good test. Also, isn't bash disallowed in system scripts like rc.local, or is that old info, or just wrong..? Might be worth trying to change bash to sh in the tplink-start.sh script because it doesn't use any bash features anyway.

Yes, and so it's also unnecessary on the command line, like quoted above.

It's because it's more than simply a javascript interpreter/jit compiler, although in the case of "npm start" the only thing it does is look up the relevant "start" directive in an accompanying package file and invoke node to execute the file listed there.

Join the Discussion

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

Didn't find your answer?

Ask the community — no account required