How to manage Linux Services with the systemctl command

Linux is an optional multitasking system like almost everyone these days. And to make this a real experience, many applications are handled in the background through services. Well, at various times and circumstances the system user needs to start, stop or restart these services. That’s why, in this post, you will learn how to handle services in Linux with the systemctl command.

Also, in this post, we will show you many of the utilities that the command can do. Besides this, we will explain what is a service and how we got to systemctl.

What is the difference between systemd and the systemctl command?

Before you take a look at using the systemctl command, it is a good idea to establish the difference between the command and systemd.

With the main objective of optimizing the management and loading of Linux services, systemd was born. It replaces the old Sysvinit not without a dose of controversy.

Systemd is composed of a series of SysV and LSB compatible daemons and scripts. It also allows the start of the daemons on demand, monitoring of the processes with the use of the Linux control groups.

On the other hand, systemctl is a command used to manage system services. It works as a kind of interface for systemd. Now it does. We are going to use systemctl.

Managing services in Linux with the systemctl command

If you use a modern Linux distribution, then you have installed and running systemd and therefore the systemctl command.

The command itself is very easy to use. Therefore the basic syntax is as follows.

systemctl [OPTIONS...] {COMMAND} ...

Like most commands in Linux, it has options that modify the behavior of the system. If you display the systemctl help, you will see a lot of options and commands available.

:~$ systemctl – help

This is because although systemctl is used to manage system services, it also allows you to perform other operations, such as shutting down or rebooting your computer.

However, you will study the management of services with systemctl through situations.

1. List the services of the system

First, you have to take into account that the command systemctl has to be used as the superuser. Or at least that your user can use sudo. Then, open a terminal and to list the system services under systemd, run the following command

:~$ sudo systemctl

As you can see from the screen output, it is a bit cumbersome. But you can still visualize the services of the system.

However, it is possible to add the list-unit sub-command and a filter to improve the presentation of the results.

:~$ sudo systemctl list-units – type=service – all

This image presents clearer results. The most important columns are the load, active and sub-columns, where it is shown if it is active in the system and if it is running.

If you only want to show the ones that are running, just modify the previous command for this one:

:~$ sudo systemctl list-units – type=service – state=running

2. Start, stop and restart services with the systemctl command

Now that you know what services are in your system, you can choose to start them, if they are stopped, or stop them if they are running.

To start a service, use this command:

:~$ sudo systemctl start [service]

Replace service for the name of the service. In the case of MySQL, the command would be as follows:

:~$ sudo systemctl start mysql

On the contrary, if you want to stop it, use the stop sub-command and the name of the service.

:~$ sudo systemctl stop [service]

Following the example of MySQL, it would look like this:

:~$ sudo systemctl stop mysql

On the other hand, there are times when you have to make changes in the configuration of some service. For these changes to be applied, you need to restart it.

:~$ sudo systemctl restart [service]

The service can also be reloaded. In this case, only partially reconfigure the service. The sub-command is “reload”.

:~$ sudo systemctl reload [service]

To restart it completely you have to use the option restart.

3. Enable and disable service to start or not at the system boot

When a third-party service is very important for the operation of our server Or some of those applications, it is convenient to ask systemd to start it up together with the system.

To do this, just follow the following syntax:

:~$ sudo systemctl enable [service]

So, replace [service] with the name of the service you need.

To disable it, use the disable sub-command.

:~$ sudo systemctl disable [service]

This way you can set which services will start automatically and which will not.

4. Masking and unmasking a service with Systemctl

If you want a service to be disabled but cannot be enabled automatically or manually, it should be masked. Do not worry that it can always be unmasked and re-enabled.

So, to mask a service, use the following command

:~$ sudo systemctl mask [service]

And to reverse the previous command:

:~$ sudo systemctl unmask [service]

This adds an extra layer of security for server management. Ideal for work sharing. Because to start or enable a masked service, you have to unmask it first.

5. Check the status of a service with the systemctl command

It is often useful to know what the status of a service is. This is to detect possible errors.

To do this, use the following command:

:~$ sudo systemctl status [service]

If the service is running, you will see an image similar to this.

If not, you will see something like this.

This is very useful to know if a certain service is running.

Conclusion

Managing services might be a bit complicated from the terminal, but it isn’t. And this helps our system to be managed correctly by the user.

In this post, you have used the systemctl command for the basic management of these services. However, the examples you just observed, are the most common cases, so they will be enough.