How to start a Node.js app on system boot?

前端 未结 3 2099
北海茫月
北海茫月 2021-02-04 12:53

I\'m working on a Raspberry Pi running Raspbian running a Node.js app and trying to get it to start when the Pi boots. I found a couple of examples but I can\'t seem to get it

相关标签:
3条回答
  • 2021-02-04 13:25

    Mohit is right, but just for clarification, you can use readlink to find the full path for your Node.js app as it will be needed later to add as a cron job.

    readlink -f <<name of file >>
    

    For instance readlink -f HAP-NodeJS/Core.js results in /home/pi/HAP-NodeJS/Core.js

    You can also use which node to find the full path where node.js is installed

    Next, create a new cron job using sudo crontab -e and add the following code at the very end:

    @reboot sudo /usr/local/bin/node <<.js application path>> &
    

    for instance, my code looks like this.

    @reboot sudo /usr/local/bin/node /home/pi/HAP-NodeJS/Core.js &
    

    Upon reboot (or start up) , your Node.js should run. Hope this clears things.

    0 讨论(0)
  • 2021-02-04 13:40

    If you're using a prebuilt Pi release like 0.10.24, you may be experiencing a PATH issue.

    You can either provide the full path to the node binary as part of the start command or make sure the PATH to the node binaries are set before /etc/init.d/MyApp is ran. I had the same issue and tried both with success. Also, the stop command as you have it may not be working.

    #! /bin/sh
    # /etc/init.d/test
    
    ### BEGIN INIT INFO
    # Provides:          test
    # Required-Start:    $remote_fs $syslog
    # Required-Stop:     $remote_fs $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Example initscript
    # Description:       This file should be used to construct scripts to be
    #                    placed in /etc/init.d.
    ### END INIT INFO
    
    # Carry out specific functions when asked to by the system
    case "$1" in
       start)
        echo "Starting test.js"
        # run application you want to start
        #node /home/pi/test.js > /home/pi/test.log
        /home/pi/downloads/node-v0.10.24-linux-arm-pi/bin/node /home/pi/test.js >> /home/pi/test.log
       ;;
       stop)
        echo "Stopping test.js"
        # kill application you want to stop
        killall -9 node
        # Not a great approach for running
        # multiple node instances
        ;;
      *)
        echo "Usage: /etc/init.d/test {start|stop}"
        exit 1
        ;;
    esac
    
    exit 0
    

    If you'd like to do sudo node, you can add the PATH to Defaults secure_path using sudo visudo.

    Also, I would recommend using something like forever to keep your process running after crashes and what not.

    0 讨论(0)
  • 2021-02-04 13:40

    I solved this problem by first checking where node.js was installed on RaspberryPi:

    which node
    

    This gave me :

    /usr/local/bin/node
    

    Open crontab config:

    sudo crontab -e
    

    Then in my crontab :

    @reboot sudo /usr/local/bin/node <complete path to your .js app> &
    

    Save, reboot, and problem solved !

    0 讨论(0)
提交回复
热议问题