Learn to create your own services in Linux using Systemd

Nowadays the vast majority of applications depend on Systemd to create their own daemons, or also known as “daemon” or simply services in the system. When we install a new program, if it has to be executed at the start of the operating system, it will directly incorporate a file specifically designed so that systemd recognizes it and starts it every time we start the operating system, however, it is very possible that not all the Applications incorporate this functionality, and if we want them to boot with the operating system, we must create a specific configuration ourselves so that it starts automatically and is constantly in the system.

In the following example that we are going to show you, we are going to create a service for the Tiny Tiny RSS update engine, although by making a few slight modifications that we will indicate later, we will be able to use it to execute any script, command or service that we we need to run in the background in our operating system with Systemd.

One of the strengths of Systemd is that it works exactly the same on all Linux-based operating systems, it doesn’t matter if you use Debian, Ubuntu or any other distribution, it’s exactly the same on all of them, and the same configuration file will serve you.

Create your own service or daemon

The first thing we must do is enter the path where all the Systemd services are located, in our case we are going to give you the examples using Debian, but the path in the different operating systems is exactly the same, there should not be any difference.

The route where we must go to see each and every one of the services is “/etc/systemd/system/”, therefore, if we use the “cd” command we will be able to go to this route without problems:

cd /etc/systemd/system/

If we do an “ls” to list all the services, we will be able to see all the ones that are created by default. Depending on the programs you have installed, we will have some services or others.

Once we are in the correct path, we will now have to create a file that ends in “service”, as you can see in the previous screenshot. All the configuration to be carried out must be in this configuration file, in our case we are going to create a file called «ttrsupdate.service» executing the command:

sudo nano ttrssupdate.service

In case you want to use vi or vim you can also do it without problems, it does not matter what text editor you use, the most important thing is the content of the file to create the custom daemon, and that they have the “service” extension as you have viewed. We must bear in mind that to create a file in this path, it is completely necessary to be root to create it, or use “sudo” to be able to create it properly.

The content of the file, in this example, is as follows:

Description=Daemon TTRSS Auto-Update Feeds mysql.service

We must bear in mind that this service will always start with the operating system, unless you decide not to.

Next, we are going to show you what each of the parts of the previous configuration file means:

  • Description: we can enter here the name and a description of what our daemon is going to do, in this case we can put what we want, it is a description for the system administrator, to know what the daemon does without having to look at more code.
  • Despues de: We will indicate if we want it to be loaded after other services or system components. This is something very important, because if our service requires another service to be up, then we will have to follow a specific sequence. There is also another directive called “Before” that does the opposite, indicating that our service is executed before that other service that we specify.
  • User: the user of the operating system that will run the daemon. It can also work with Group. This is very important, because depending on the permissions that the service needs, we will have to run it with root, our non-privileged user or any other system user.
  • ExecStart: We must indicate the absolute path (they do not work relative) to the script or binary that we want to execute to start the service in question.
  • WantedBy: usage directives and other dependencies.

Once we have our .service file configured, it is recommended to check that the owner of the file is root and the group as well, in addition, we should configure permissions 777 to the file in question, we can do it like this:

chmod 777 ttrssupdate.service

Now that we have it fully configured, we have to enable it and we will teach you how to manage the service.

Manage the created service

Now that we have created the service in the operating system, before we can start using it we must enable it with the “enable” command. In order to do this, we simply have to run the “systemctl enable” command followed by the name of the service we just created. It is not necessary to run this in the same path where the files are stored, in our case it would be:

systemctl enable ttrssupdate.service

Of course, it will ask us for the system’s superuser password to enable it properly, for this type of task it is essential to have superuser permissions, otherwise we won’t be able to do anything.

Now that we have enabled the service, we have to start it manually the first time. From this moment it will always be running, even when we restart the operating system, this service will also start automatically.

systemctl start ttrssupdate.service

In these moments, the service will be active in our operating system. Although systemd can be quite complicated if we get into advanced things, creating a service is not too complicated, as you have seen throughout this tutorial. Next, we are going to show you other very useful commands to manage our newly created service:

  • sudo systemctl daemon-reload: this order allows us to reload all the services again, very useful if we modify several of them, in this way, we can do it globally with all of them.
  • sudo systemctl stop ttrssupdate.service: this command allows us to stop the previously started service, by stopping the service we could make changes that could not be made when started, for example, to apply changes we will also have to stop and start the service.
  • sudo systemctl restart ttrssupdate.service: allows us to restart the service (it is the same as stopping and starting it, but in a single command).
  • journalctl -u ttrssupdate.service: allows us to see the log generated by the service in question, in case there is an error when executing it, or any warning that could lead to future problems.
  • sudo systemctl status ttrssupdate.service: allows us to see the status information about the service in question, this command is essential to see if it is running or not.

As you have seen, creating a service with systemd is quite simple, and managing it using the different commands that we have indicated is also quite simple.

Related Articles

Leave a Reply

Your email address will not be published.