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

    From NY@3:633/10 to All on Thu Nov 12 13:34:26 2026
    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.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Richard Kettlewell@3:633/10 to All on Thu Nov 12 18:08:48 2026
    NY <me@privacy.net> writes:
    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.

    What is the output from the cron job?

    --
    https://www.greenend.org.uk/rjk/

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Lawrence D?Oliveiro@3:633/10 to All on Fri Nov 13 12:30:00 2026
    On Thu, 12 Nov 2026 13:34:27 +0000, NY wrote:

    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.

    What exactly is the error?

    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.

    I would do this via a systemd service definition. That provides
    automatic monitoring and restarting if/when it crashes (up to some
    reasonable limit, to guard against endless loops). And any error
    messages should appear in the journal as well.

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)