I often see the need to create some program or script that needs to be always running on the machine, and also when the computer is restarted.

There are a few tools to solve this job, but always consider using solutions built into your operating system first.

It's very easy to create a Linux service, you can create a service that runs any script or program in any programming language, and make that system persistent and resilient as a service on your machine.

In this article we are going to use the systemd to manage our service, it is present in most linux distributions like Ubuntu, Debian, OpenSuse…

Creating the Program

Let's create a simple web application in NodeJs for example. he will listen at the door 9999 and return the current date.

const http = require('http');

http.createServer(function (req, res) {
  res.write(new Date().toString());

Let's test it, in the browser, go to http://localhost:999 .

Now that we validated that our program works, let's make it run always, if for some reason it stops working it must be restarted and if the machine is restarted, the service needs to start too.

Creating the Service

I will call our service webdate , with your preferred terminal editor creates a file in the path /etc/systemd/system/webdate.service .

Description=WebDate - Date Web Service
ExecStart=/usr/bin/node /path/file.js


You will need to change the following variables:

  • User= : I put your current user, it will be the user who will run the service
  • ExecStart= : This is the path to the script that will be executed, you need to provide the full path to the command line first in, normally, /usr/bin/node and then the full path to your file.

Now to start our service just run the following command:

systemctl start webdate

To enable automatic startup when the system is turned on:

systemctl enable webdate

Okay, now your service is running and configured to restart in case of failure and start automatically with the system. To stop the service just run the following command:

systemctl stop webdate
0 0 votos
Nota do Artigo
Notify of

0 Comentários
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x