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

前端 未结 3 2103
北海茫月
北海茫月 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 <>
    

    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.

提交回复
热议问题