- Running Node.Js as a systemd service
- 1 — Installing Node.js
- 2 — Setup a user
- 3 — Checkout the the app’s repo
- 4 — Create a systemctl service
- Node.js Application as a Service
- Saved searches
- Use saved searches to filter your results more quickly
- natancabral/run-nodejs-on-service-with-systemd-on-linux
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
Running Node.Js as a systemd service
systemd is a software suite that provides an array of system components for Linux operating systems.
For us, it lets you run services, start them on boot then check and manage the status of them. This is useful for making sure a node.js script is running whenever a raspberry pi is running. To run through this I’ll be installing a node.js app called blinkit .
1 — Installing Node.js
Assuming you’re starting from scratch, say from Raspbian lite, you’ll need to install node first.
VERSION=v10.20.1-linux-armv6l
# Create and enter a temporary directory
TMP=`mktemp -d`
cd $TMP
# In the temp dir, get the node binaries and extract them
curl -sLO https://nodejs.org/dist/latest-v10.x/node-$VERSION.tar.gz
tar -xzf node-$VERSION.tar.gz
mv node-$VERSION /usr/src/node
# Link the binaries onto the $PATH
ln -s /usr/src/node/bin/node /usr/bin/node
ln -s /usr/src/node/bin/npm /usr/bin/npm
ln -s /usr/src/node/bin/npx /usr/bin/npx
# Clean the temporary directory
rm -r $TMP
Feel free to change the version of course, I used this older one as it supports armv6 on the Pi Zero I was installing on.
2 — Setup a user
We’ll need a system user that the app will run as, we’ll keep it simple and call them node . Then we’ll give them sudo and gpio access (this is a special group on Raspberry Pis) and create a folder for the app to be in.
# Create the unix user
adduser node
# Add them to groups
usermod -aG sudo node
usermod -aG gpio node
# Create a node-owned folder to put the app in
# You call this whatever you like, probably the name of your application
mkdir -p /usr/src/blinkit
chown -R node:node /usr/src/blinkit
3 — Checkout the the app’s repo
Now you need to get the code that you want to run, and put it in that new folder.
# Become our new node user
su node
# Clone the repo
git clone git@github.com:robb-j/blinkit.git /usr/src/blinkit
cd /usr/src/blinkit
# Install the app's production dependencies
# There could be trouble here if any of your dependencies require extra binaries, like python
# You might need to debug you packages and see what they require a bit
npm install --production
4 — Create a systemctl service
To run with systemd we need a file which tells systemd how to run and manage our service. For blinkit I put this file inside the repo which is pulled down, so it is available at /usr/src/blinkit/blinkit.service
blinkit.service
[Unit] Description=blinkit Documentation=https://github.com/robb-j/blinkit/ After=network.target [Service] Type=simple User=node ExecStart=/usr/bin/node /usr/src/blinkit/src/cli.js serve WorkingDirectory=/usr/src/blinkit Restart=on-failure [Install] WantedBy=multi-user.target
This service file is what systemd uses to know our application exists and what to do with it. You should change the Description , Documentation , ExecStart and WorkingDirectory statements to match your application.
Next we need to tell systemd about our new service. We will symlink the service file into the systemd directory, then restart the daemon and enable and start the new service.
# Your service name will be what you set `Description` to above
SERVICE=blinkit
# Link the service file into place
sudo ln -s /usr/src/blinkit/blinkit.service /lib/systemd/system/blinkit.service
# Reload the daemon so it knows about the new file
sudo systemctl daemon-reload
# Enable our new service
sudo systemctl enable $SERVICE
# Start the service
sudo systemctl start $SERVICE
Now the new service should be running and doing whatever it does. You can check its output with:
# -f follows the logs in real time
# -u reverses the order so its newest first
journalctl -fu $SERVICE
If you restart your host, the service should now start up during the boot. Congratulations! 🎉
Node.js Application as a Service
Let’s assume you are creating a service Node.js application, i.e. an app that is supposed to start at boot and keep running for as long as your system is up. Achieving this requires an additional piece of software that will perform the actual job of starting and re-starting your app. One choice you have is called Forever.
Forever is the process manager of the Case Node.js — a Node.js runtime that is available on several major operating systems. There is nothing particularly bad about Forever, except that it is yet another Node.js process that will take up RAM (about 30MB), NAND space, and CPU resources while only providing a single primitive function: starting your program and restarting it after a failure.
A more rational choice for the LTPS is called SystemD. This is a system-wide, feature-rich, flexible, and modern Linux service manager that has everything you need: service startup dependencies, status control, restart on events, D-Bus integration, activation on connect, and so on. SystemD is very popular, and many articles on the Node.js and SystemD integration are available on the Internet. As an example, see this article and this one, too. These are written by people who work with Node.js services on a «pro level.»
Here is an example of using SystemD for turning your app into a service. It assumes that your app.js is at /opt/node-apps/demo0/site/.
# copy this file to /lib/systemd/system/ [Unit] Description=LTPS NodeJS Test Application After=network-online.target [Service] Restart=on-failure WorkingDirectory=/opt/node-apps/demo0/site/ ExecStart=/usr/bin/node /opt/node-apps/demo0/site/app.js [Install] WantedBy=multi-user.target
Anything your new myapp service outputs to the error or standard output stream will be redirected to the system journal. So, if something goes wrong you may use our System Journal Web Viewer to see the stack trace messages generated by Node.js exception handlers or console.log() output.
- You may also find it convenient to see journaled messages directly in the CLI (To cancel this from the journal reader, press CTRL+C):
- You may also want to set the main working directory for your service and limit the resources it takes from the OS:
[Unit] Description=LTPS NodeJS Test Application After=network-online.target [Service] Restart=on-failure # do chdir before running the service WorkingDirectory=/opt/node-apps/demo0/site/ ExecStart=/usr/bin/node app.js # limit CPU and RAM quota for our service CPUAccounting=true CPUQuota=10% MemoryAccounting=true MemoryLimit=50M [Install] WantedBy=multi-user.target
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Node.js as a running service is becoming more and more popular these days. One of the issues many developers face is how to ensure their node.js service starts automatically, and more importantly how to keep it running should it crash.
natancabral/run-nodejs-on-service-with-systemd-on-linux
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Run node.js service with systemd on Linux
Node.js as a running service is becoming more and more popular these days. One of the issues many developers face is how to ensure their node.js service starts automatically, and more importantly how to keep it running should it crash.
Using systemd, which makes this process a lot simpler and more efficient, and means that we do not need another scripts to run your server node.js.
const http = require('http'); const hostname = '0.0.0.0'; // listen on all ports const port = 3311; http.createServer((req, res) => res.writeHead(200, 'Content-Type': 'text/plain' >); res.end('Hello World'); >).listen(port, hostname, () => console.log(`Server running at http://$hostname>:$port>/`); >);
We will save this file (for example) as /home/myserver/server.js . Verify the node server works on terminal:
$ node /home/myserver/server.js # or nodejs, maybe $ nodejs /home/myserver/server.js
Create the service file on Systemd
Create a service file on /etc/systemd/system/ or /lib/systemd/system/ (etc|lib).
$ cd /etc/systemd/system/ $ nano myserver.service
[Unit] Description=My Little Server # Documentation=https:// # Author: Natan Cabral [Service] # Start Service and Examples ExecStart=/usr/local/bin/node /home/myserver/server.js # ExecStart=/usr/bin/sudo /usr/bin/node /home/myserver/server.js # ExecStart=/usr/local/bin/node /var/www/project/myserver/server.js # Options Stop and Restart # ExecStop= # ExecReload= # Required on some systems # WorkingDirectory=/home/myserver/ # WorkingDirectory=/var/www/myproject/ # Restart service after 10 seconds if node service crashes RestartSec=10 Restart=always # Restart=on-failure # Output to syslog StandardOutput=syslog StandardError=syslog SyslogIdentifier=nodejs-my-server-example # #### please, not root users # RHEL/Fedora uses 'nobody' # User=nouser # Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody' # Group=nogroup # Variables Environment=PATH=/usr/bin:/usr/local/bin # Environment=NODE_ENV=production # Environment=NODE_PORT=3001 # Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN" # Environment="ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd" [Install] WantedBy=multi-user.target
Enable the service on boot
$ systemctl enable myserver.service
$ systemctl start myserver.service $ systemctl restart myserver.service $ systemctl status myserver.service
$ journalctl -u myserver.service
Reload all services if you make changes to the service
$ systemctl daemon-reload $ systemctl restart myserver.service
Maybe we need permissions
$ chmod +x /etc/systemd/system/myserver.service $ chmod 664 /etc/systemd/system/myserver.service
To kill process if you need try