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.jsOutput:
- Server running at http://0.0.0.0:3311/
Create a service file on /etc/systemd/system/ or /lib/systemd/system/ (etc|lib).
$ cd /etc/systemd/system/
$ nano myserver.serviceFile location:
- /etc/systemd/system/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
$ systemctl enable myserver.service$ systemctl start myserver.service $ systemctl restart myserver.service $ systemctl status myserver.service
$ journalctl -u myserver.service
$ systemctl daemon-reload $ systemctl restart myserver.service
$ ps -ef | grep myserver.js$ chmod +x /etc/systemd/system/myserver.service $ chmod 664 /etc/systemd/system/myserver.service
$ kill -9 {numberPID}ToDo
- Dev NPX - to create services files and save. prompt and readline.
Names to remember
- systemctl - system control
- systemd - system manager standard
- ps - process
- chmod - change the mod