As most system services are started at boot time, some are not. The system administrator can stop a service that is not frequently used and as well start it when needed. It is required to restart a service after making changes to the configuration file.

In this article, you will learn how to control system services with practical examples using systemctl

HOW TO STOP A SERVICE FROM RUNNING

If you want to stop a running service, you do the following.

We just stopped the chronyd service, and the service shows “inactive and dead.” The chronyd service is responsible for time and date synchronization

HOW TO START A SERVICE

To start a stopped service, we use the start option. Below command shows how to start the already stopped chronyd service

Please notice the process ID of the command when we stopped the service and when it was started. The process ID changed. Each time you stop and start a service, the process ID of the service will change. This is also applicable to restarting a service. Please, follow the next example to know the similarities

HOW TO RESTART A SERVICE

Restarting a service is more like stopping and starting a service in a single mode. Please focus on the state of the process and also the process ID.

You noticed the change in the process ID when compared to the previous command

HOW TO RELOAD A SERVICE

You can issue your system to read your configuration files and reload the service without totally stopping and starting the service. With this, the process ID of the service will not change.

Let's view the status of our sshd service to note its state and the process ID

This is the process ID of our sshd service. Now lets reload the service

You noticed the service retained its process ID before and after the reload

UNDERSTANDING UNIT DEPENDENCIES WHEN CONTROLLING SYSTEM SERVICES

Before understanding this, please list the unit type of your cup service and cup socket using the command systemctl [list-units] [- -type] [socket] and systemctl [list-units] [- -type] [service].

Below is the result;

You can see the unit type of cup (cup.service and cup.socket). Services may be started as dependencies of other services. When a socket unit is enabled, and the service unit is not, the service unit will automatically start when the request is made on the network socket. This is sometimes applicable to path units.

Check below commands for better understanding of unit dependencies

In the above command, the cup service and the cup socket is active and running.
Let’s try to stop only the cup service and see the result.

To stop the cup services completely, you have to stop all three units (cups.path, cups.socket and cups.service).

HOW TO LIST UNIT DEPENDENCIES

To list a tree of unit dependencies, you use the [systemctl] [list-dependencies] [UNIT]. The list displays other units that are dependent on some units before they can be started

The truncated result from the command. These are the units that must be started when the cups service is started

HOW TO MASK A SERVICE

A service is masked to prevent an administrator from accidentally starting that service. Masking will create a link in the configuration directories so that the service will not be started manually or automatically.

The syntax goes as ;

[systemctl] [mask] [UNIT]

This is how masking is done. It creates a link, and even when you try starting it manually, the service will not start until you unmask the unit.

The below command will unmask the sshd service by removing the link created in the configuration directory

HOW TO ENABLE AND DISABLE DAEMONS

Starting a service on a running system do not guarantee that that service will be started when the system reboots. Also, stopping a service on a running system does not guarantee the service will be stopped when the system reboots. Services are started at boot time when links are created in the configuration directory. When these links are not created at boot time, the administrator will have to start does service manually when needed using the systemctl command.

Let's try disabling a running service

You see no effect on the state of the service, but the service is disabled. Basically, services can be started and stopped on a running system but to prevent a service from starting automatically at boot time, you will have to disable it or enable it when you want to it to start at boot time.

Note that: To disable a service is not the same as masking a service. As a disabled service won't start automatically at boot time but can be started manually when a request is made using the systemctl command, but a mask service cannot be started manually or automatically until you unmask the service.

RECAP

systemctl [start] [UNIT] will start a service

systemctl [stop] [UNIT] will stop a service

systemctl [reload] [UNIT] will reload the configuration file of a service without making changes to process ID

systemctl [restart] [UNIT] will restart the service. It's just like stopping and starting a service at the same time. It allocates a new process ID as in when starting and stopping a service

systemctl [status] [UNIT] will view the information about a service

systemctl [mask] [UNIT] will disable a service from starting manually and automatically

systemctl [unmask] [UNIT] will enable a mask service

systemctl [list-dependencies] [UNIT] will list units that depend on other units to function

systemctl [enable] [UNIT] will enable a service to start at boot time

systemctl [disable] [UNIT] will disable a service to start at boot time

Author

Comments