Search code examples
node.jslinuxbashraspbianboot

Run node app and jar file on boot Raspbian


I have two files, one that contains a NodeJS server and one file .jar that contains a Java server.
I need to run both of them when my system boots. I'm on Raspbian on RaspberryPi 4.

I created a file called serversBoot which content is:

#!/bin/bash

java -jar server.jar
node webServer.js

The Java server starts correctly, but the node one doesn't seem to.
Also I have no idea how to make this script run as soon as my Raspberry boots
I can assure that running in the terminal the same commands I put in the bash script works just fine.
What am I doing wrong here?


EDIT
When the commands are put into two different files it works, it doesn't work when put in a single file.


Solution

  • You have to start each command as a separate process. In your script java -jar... command is blocking the execution of anything else for as long as that process is running.

    Try killing the java process to see if node webServer.js starts.

    To start both, just ask bash to send each process to background:

    #!/bin/bash
    
    java -jar server.jar &
    node webServer.js &
    

    To manage these 2 processes a bit better you should capture their PIDs, so that you know which process to look at. You can get the PIDs like this:

    #!/bin/bash
    
    java -jar server.jar & java_pid=$!
    node webServer.js & node_pid=$!
    echo $java_pid > java.pid
    echo $node_pid > node.pid
    

    This way .pid files contain the process IDs of the started processes.

    See more info on starting and managing processes from bash scripts here