DEV Community

terpinmd
terpinmd

Posted on

1

Node app as a service

Recently I deployed a node application to a raspberry pi that i had laying around. There was surprisingly sparse documentation on how to start the app as a service so that when the ssh connection was shut down the app would still run.

First I wrote a small script and placed in in the pi users home directory:

~/startup (chmod 755)

export NODE_OPTIONS="--max-old-space-size=2048"
export PATH=$PATH:/path/to/node/bin (and whatever else you want on path)
npm run prod

Test the file by executing it:

sh ~/startup

At this point the app was up and running. Its important to not move on until the startup script is working.

What I then wanted to do was to be able to run this as a service like:

sudo service myservice start

The following is that code which I then placed in /etc/systemd/system/:

/etc/systemd/system/myservice.service

[Unit]
Description=myservice

[Service]
ExecStart=/home/pi/startup
Restart=on-failure
RestartSec=60s
User=pi
Group=nogroup
WorkingDirectory=/path/to/app
RestartSec=30000ms

[Install]
WantedBy=multi-user.target

Many of the above properties are not mandatory. The key is the ExecStart which references the startup file we created. Finally, I wanted the service to start on boot which is done via:

sudo systemctl enable myservice

The service can be start, stopped and monitored with:

sudo service myservice start
sudo service myservice stop
sudo service myservice status

It took me a while to get this to work but once I found the pieces it was very easy.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more