×

Search anything:

Systemctl and Systemd in Linux

Free book on Dynamic Programming

Get this book -> Problems on Array: For Interviews and Competitive Programming

The systemctl command is a systemd utility used to manage services, get information about service unit files and service states, and therefore a useful utility to know for managing services on the server while systemd is an array of components for Linux OS.

Table of contents.

  1. Introduction.
  2. Managing services.
  3. Managing units.
  4. Systemctl shortcuts.
  5. Summary.
  6. References.

Introduction.

The systemctl command in a Linux utility is used to manage the systemd service and service manager.
systemd is an init system and system manager, It runs with PID 1 and it is the one responsible for starting the rest of the system.

system1-3

The syntax is as follows:

systemctl [OPTIONS...] COMMAND ...

system3

Managing services.

The init system is responsible for the management of services and daemons at any point the system is running.
The target of most systemd actions is units - these are resources that systemd knows how to manage. They are mostly categorized by the type of resource they represent.

For managing services the units are service units, these have unit files with suffix .service.

In our examples we will be using MariaDB service but feel free to use any other application e.g apache2, MySQL, etc:

Starting, stopping, restarting, reloading services.

To start a service, say apache2
We write:

$ sudo systemctl start mariadb.service

We can also ignore the .service suffix:

$ sudo systemctl start mariadb

To stop the service we write:

$ sudo systemctl stop mariadb.service

Usually, after changing a configuration for a service we need to restart it, with systemctl, we restart the service as follows:

$ sudo systemctl restart mariadb.service

Instead of stopping then starting(restarting), we can also reload a service:

$ sudo systemctl reload mariadb.service

We can also combine reloading and restarting, that is if we are not sure about the service functionalities.

$ sudo systemctl reload-or-restart mariadb.service

Service statuses.

If we are not sure if a service has started after trying to start it we check its status as follows:

$ sudo systemctl status mariadb.service

system4

Using systemctl with the is-active argument allows us to check if a unit is active/running.

$ sudo systemctl is-active mariadb.service

system6

The is-enabled argument is used to check if a service starts when the machine boots up:

$ sudo systemctl is-enabled mariadb.service

system5

If we started a service and it fails, we can check this by writing:

$ sudo systemctl is-failed mariadb.service

system7

Enabling and disabling services.

Sometimes we use services frequently so it is convenient to start the service automatically immediately the system boots up.
For this we use enable with systemctl:

For example, to start mariadb service every time the system boots up we write:

$ sudo systemctl enable mariadb.service

system8-2

After the command is executed a symbolic link is created in the /lib/systemd/system directory.

To undo the above operation, we use disable:

$ sudo systemctl disable mariadb.service

system9

Managing units.

We have seen how to manage single services using systemctl, In this section, we will use additional systemctl commands to explore the current state of the system.

For example, to list current units, we write:

$ systemctl list-units

system10

We can also list units by writing:

$ systemctl

Notice in the above command that we pass no arguments.

The image above has five columns:
UNIT: this is the systemd unit name.
LOAD: specifies if the unit loaded correctly, that is, if the unit was parsed successfully by systemd.
ACTIVE: specifies whether a unit is active or not.
SUB: This displays the low-level activation state. Values here are dependent on the type of unit.
DESCRIPTION: This is the description of the unit file.

Now, let's filter out all services that failed:

$ systemctl list-units --all --state=failed

Unit files are .ini files that encode information about services, mount points, swap files and partitions, startup targets, sockets, devices, etc.
They are loaded from a set of paths that are determined during compilation.

Let's list all unit files:

$ systemctl list-unit-files

system11

To list the unit file for cron, we write:

$ systemctl cat cron.service

system12-1

And to list the properties of a service:

$ systemctl show cron.service

system13

We can also change configurations by editing unit files:
For example, let's edit the MariaDB unit file and change the nice value:

$ sudo systemctl edit --full mariadb.service

system14-1

Systemctl shortcuts.

We can also use systemctl to perform actions such as halting, rebooting, shutting down, putting the system in rescue mode, etc:

$ sudo systemctl halt
$ sudo systemctl reboot
$ sudo systemctl shutdown

Summary.

The systemctl command is the central management tool used to control the init system, we have seen how to manage services, check their statuses, work with configuration files, and change system states all by using this command.

References.

  1. Execute the command $ man systemctl for its manual page.
Systemctl and Systemd in Linux
Share this