I have done IoT project with Raspberry pi (Raspberry pi 3). Server was built with node (express/SQLite3). I wanted to run my server on startup and I had to do that by using bash script. I have tried that in this way

case "$1" in
        echo "Starting Server"
        cd /home/pi/Documents/path/
        npm start

After that I added this script to the startup by using this command

$ sudo update-rc.d script_name defaults

(My file name was “script_name”)

It works several times. But the case is most of the time it’s not working. I couldn’t see the port number in netstat. When it runs manually it shows post 3030 is already used. As I thought,when I start server once the server assigned a port. The case is after reboot the pi the server port is not shutdown properly. So that I used a way to avoid that and it works properly for me.

case "$1" in
    start )
        echo "Checking server"
        # Identify if there is already assigned port and take the pid of it
        pid="$(pidof node ./server.js)"
        echo $pid
        # kill the already assigned port
        sudo kill $pid
        echo "Starting Server"
        cd /home/pi/Documents/path/
        npm start


After doing that modification reboot pi.

It check, when start the raspberry if there is already assign the port for the particular server and take the process id of server, then kill the process. After completing the killing process run the “npm start”.

It works properly for me. 🙂



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s