• Starting a node/npm program at boot time

    From NY@3:770/3 to All on Mon Sep 19 20:00:02 2022
    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?

    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Dennis Lee Bieber@3:770/3 to All on Mon Sep 19 18:50:26 2022
    On Mon, 19 Sep 2022 20:00:02 +0100, "NY" <me@privacy.invalid> declaimed the following:


    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?

    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.



    --
    Wulfraed Dennis Lee Bieber AF6VN
    wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From Computer Nerd Kev@3:770/3 to me@privacy.invalid on Tue Sep 20 08:32:56 2022
    NY <me@privacy.invalid> wrote:

    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 &

    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".

    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?

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

    Is it a problem with the script expecting to run in a Terminal
    window that continues to have stdout?

    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.

    --
    __ __
    #_ < |\| |< _#

    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)
  • From A. Dumas@3:770/3 to Computer Nerd Kev on Tue Sep 20 12:03:24 2022
    On 20-09-2022 00:32, Computer Nerd Kev wrote:
    NY <me@privacy.invalid> wrote:

    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 &

    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".

    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.

    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?

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

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

    Is it a problem with the script expecting to run in a Terminal
    window that continues to have stdout?

    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.

    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.

    --- SoupGate-Win32 v1.05
    * Origin: Agency HUB, Dunedin - New Zealand | Fido<>Usenet Gateway (3:770/3)