Forever Module
is good, but you don’t need another software to control background processes on your server!
One of the challenges in production is how to
All of them are similar to every long running application on server side, so the Linux community have that kind of tools natively integrated into almost every linux distribution. One of the latest tools for handling and processing long running background tasks/services is called SystemD
, which is developed by Linux Foundation and now it became standard for Linux distributions. We will be focusing only on Ubuntu Server because it’s really easy to get started with it.
SystemD is an init system used in Linux distributions to bootstrap the user space and manage all processes subsequently — from Wikipedia
This means that SystemD is actually developed to manage and execute User Space processes/services. You can schedule a job, start/stop/restart it and collect it using system logs.
Base command to get started with it is systemctl
which have all sub commands to manage existing services or to add exiting ones.
To make this experiment more realistic, making very basic node.js hello world http listener.
#!/usr/bin/env node
/** server.js */
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.var server = http.createServer(function (request, response) {response.writeHead(200, {"Content-Type": "text/plain"});response.end("Hello World\n");});
// Listen on port 8000, IP defaults to 127.0.0.1server.listen(8000);
// Put a friendly message on the terminalconsole.log("Server running at http://127.0.0.1:8000/");
Take a look on the first line of our server.js
file, using that line we are just letting know that using current user environment we will run this file with node
executable.
Saving that file to ~/hello_world/server.js
and making it as an executable
chmod +x ~/hello_world/server.js
It’s time to make SystemD Service
Making file /etc/systemd/system/hello_world.service
, this would be the main file as a configuration to SystemD service
[Unit]Description=Node.js Hello World Http Server
[Service]PIDFile=/tmp/hello_world-99.pidUser=<Your Username>Group=<Your User Group>Restart=alwaysKillSignal=SIGQUITWorkingDirectory=/home/<username>/hello_world/
ExecStart=/home/<username>/hello_world/server.js
[Install]WantedBy=multi-user.target
After saving this file, we can enable this service, to start using it over SystemD
sudo systemctl enable hello_world.service
That’s it! Now you have Node.js service as a background service, it will be restarted when something goes wrong and you can view all output logs using very simple command
sudo journalctl -u hello_world.service
# For real time logs just add -f argumentsudo journalctl -fu hello_world.service
After all this setup kind of things now you got very easy way to manage your service
sudo systemctl start hello_world.servicesudo systemctl stop hello_world.servicesudo systemctl restart hello_world.service
So you actually don’t need modules like forever
for managing Node.js background services, it all could be done using Linux native tools, which are more performant and more reliable! (hope so).
So if you learned something from this, just _Recommend This_
for letting to learn others too.