+44 74 1835 1231

How To Control Service In CentOS 7 with Systemd

Tutorial

August 20, 2020 by Emery

Control Service In CentOS 7 with Systemd

The ability to manage services through Systemd is an essential element in the overall control of a Linux server. Different tools are used in various Linux distributions to manage services. In the distribution of CentOS 7, the Systemd tool is used for this purpose. This article is created with the aim of examining how to control systems and services in operating systems that run the Systemd tool.

What Is Systemd?

Systemd is a manager for Linux systems and services that is compatible with all LSB and SysV systems.

What Are The Benefits Of Systemd?

  • Systemd provides dynamic parallel processing capability.
  • Uses D-bus activities and sockets to start services.
  • Start on-demand services.
  • Tracks processes using the Linux control group.
  • Supports snapshotting and restore system mode.
  • Hold the mount and automount points and implement a service control logic based on interdependence.

In Systemd, all services, sockets, mount points, devices are called units. In the Linux ecosystem, Systemd is implemented in most Linux distributions with a few exceptions. Systemd has replaced SysV init in centos 7, which makes the operating system boot faster because Systemd uses fewer scripts and also executes most tasks simultaneously (in parallel).

What Is Systemctl?

Systemctl is a system tool for managing and controlling Systemd.

Get Started With Systemd And Systemctl

For the first step to start working with Systemd, we use the following command to check whether Systemd is installed on the Linux distribution and what version of it is installed on our Linux operating system.

#debian based system
systemd --version
#rhel based system
systemctl --version

check systemd or systemctl version

The output of the above command will be similar to the following if Systemd is enabled on your working distribution, in which Systemctl version 219 is enabled.

systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETU P +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

If you see another message like -bash: Systemd: command not found, it means that Systemd is not used in your distribution or version and is probably still running on SysV, like Centos 5.

Check That Systemd Is Active Or Not

ps -eaf | grep [s]ystemd

If after executing the above command you do not see the output, of course, if Systemd is active in your system, this is almost impossible, except in special and possibly critical circumstances, it means that Systemd is disabled. And is not active, otherwise, you will see an output similar to the one below.

check systemd is active or not

Systemd Boot Process Analysis

Using the below command, you can find out the approximate boot time of the operating system which will have the same output as below.

systemd-analyze

Systemd Boot Process Analysis

Boot Analysis Of Each Unit

Using the below command, you will see the time spent per unit from the beginning of the start-up process to its completion and full activation, as shown in the output below.

systemd-analyze blame

Boot Analysis Of Each Unit

Get A List Of All Units

With the command below, you will see a list of all the units on the system as shown below.

systemctl list-unit-files

Get a list of all units with systemd

Get A List Of All Running Units

With the below command you will see all the units that are currently active, the output of which is as follows.

systemctl list-units

Get a list of all running units with systemd

List Of All Failed Units

Using the below command in the shell environment, you can easily find all the stopped units and perform the desired operation to solve their problem, which will have the same output as below.

systemctl --failed

Check Whether The Service Or Unit Is On Auto-Start

By sending the below command, you will find out whether a unit or service is enabled in your system as an automatic start or auto-load, in response to which the word enable is enabled and the word disabled will be printed.

systemctl is-enabled httpd.service

Check whether the service or unit is auto-start with systemd

Check The Status Of A Service Or Unit

To check the status of a service or unit, we use the below command, if it is not active, the output will be as follows.

systemctl status firewalld.service

Check the status of a service or unit with systemctl

And if an output service is active, you will see the same output as below.

How To Manage And Control Services Using Systemctl

So far, we are familiar with some systemctl commands and the systemd mechanism, but a very important and useful part of the systemctl command is how to manage and control services, which we will discuss below.

List Of All Active And Inactive Services Of The System

To get the list of services, we use the below command, the output of which will be similar to the following.

systemctl list-unit-files --type=service

Note: If you want to filter services with state or special status such as enabled in the above list so that only units with the desired status are displayed, you can do this:

Code: systemctl list-unit-files –type=service –state=enabled

The status of the mask in the state column of the list of services or units means it cannot be started, meaning that this unit will not be run or started manually or even at the request of other services/software.

How To Reload Services With Systemctl Start, Stop, Restart In Linux VPS

For this purpose, you can use the following commands, due to the specificity of the operation performed by each command, we ignore its explanation.

systemctl start httpd.service
systemctl restart httpd.service
systemctl stop httpd.service
systemctl reload httpd.service
systemctl status httpd.service

How To Change A Service To Active Or Inactive Status (Auto-Start At System Boot)

To start a service automatically at boot time, use the below command.

systemctl enable httpd.service

To get a service out of auto-start mode, we use the below command.

systemctl disable httpd.service command

To start non-startup mode, use the below command.

systemctl mask httpd.service command

To exit the non-startup mode use the below command.

systemctl unmask httpd.service

How To Kill A Service With Systemctl

You must know that kill in software systems means immediate stop of a process, but of course, kill itself can be executed with several different methods, each of which has its own description, which we must explain in a separate post. You can kill a service via the below command.

systemctl kill httpd

Manage Mount Points Using Systemctl

When we create a partition in Linux, to access it, we have to connect this partition to a directory. This operation is called mounting, which we will explain in detail about this and how to mount it in future articles.

The powerful systemctl tool also helps you manage the mount point, which we’ll cover later.

List Of All Mount Points

Using the below command, you get the list of mount points, the output of which will be as follows.

systemctl list-unit-files --type=mount

List of all mount points with systemctl (systemd)

How To Stop, Start, Reload And Get The Status Of A Mount Point

You can perform the mentioned operations using the following commands.

systemctl start tmp.mount
systemctl stop tmp.mount
systemctl restart tmp.mount
systemctl reload tmp.mount
systemctl status tmp.mount

How to stop, start, reload and get the status of a mount point with systemd

How To Enable Or Disable A Mount Point During System Boot (automount)

The following commands work as well as for services for automount is is-active to check the current status of a mount point.

systemctl is-active tmp.mount
systemctl enable tmp.mount
systemctl disable tmp.mount
systemctl mask tmp.mount
systemctl unmask tmp.mount

How To Manage And Control Sockets Using Systemctl

As you can see in the mount points, all the commands and functions of the systemctl command in the mount points were the same as in the previous one in controlling the services, except that in all the commands we replaced the word service with the word mount. The same applies to the management and control of sockets as all previous commands and the only socket is used instead of service.

Get A List Of Sockets Using Systemctl

To get the list of active and inactive sockets, use the below command, where you will see the output as before.

systemctl list-unit-files --type=socket

How To Stop, Start, Reload And Get The Status Of A Socket Using Systemctl

You can perform the mentioned operations using the following commands.

systemctl start cups.socket
systemctl restart cups.socket
systemctl stop cups.socket
systemctl reload cups.socket
systemctl status cups.socket

How To Enable Or Disable A Socket At Boot Time (Auto Start)

The following commands, as in the case of services, also work to auto-start sockets. Is-active to check the current status of a socket.

systemctl is-active cups.socket
systemctl enable cups.socket
systemctl disable cups.socket

Put The Socket State In Non-Startup Mode On Linux With Systemctl

Using the first command, you can put a socket in Linux in a non-executable state using Systemctl so that it does not run manually or at the request of other services, and cancel this state with the second command.

systemctl mask cups.socket
systemctl unmask cups.socket

Summary

As you can see, Systemd is a powerful tool that you can use to manage services in RHEL and Debian based systems.

I hope you’ve enjoyed this post and found it useful.

Was this post helpful?

About the Author Emery

Leave a Reply

Your email address will not be published. Required fields are marked *