Git Product home page Git Product logo

natancabral / run-nodejs-on-service-with-systemd-on-linux Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 2.0 90 KB

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.

JavaScript 100.00%
service systemd systemctl unix linux nodejs node

run-nodejs-on-service-with-systemd-on-linux's Introduction

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.

read my article

Create the node.js server

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

Output:

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

File 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

Enable the service

Enable the service on boot

$ systemctl enable myserver.service

Start the service

$ systemctl start myserver.service
$ systemctl restart myserver.service
$ systemctl status myserver.service

Verify it is running

$ journalctl -u myserver.service

Reload all services if you make changes to the service

$ systemctl daemon-reload
$ systemctl restart myserver.service

List process

$ ps -ef | grep myserver.js

Maybe we need permissions

$ chmod +x /etc/systemd/system/myserver.service
$ chmod 664 /etc/systemd/system/myserver.service

about permissions

To kill process if you need try

$ kill -9 {numberPID}

read my article

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

Author

Natan Cabral
[email protected]
https://github.com/natancabral/

run-nodejs-on-service-with-systemd-on-linux's People

Contributors

natancabral avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

kmariappan mimr9

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.