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?