Using the systemctl command to manage Systemd services

The systemctl command has six options to control a systemd service. These options are start, stop, restart, reload, enable and disable. We can categorize these options into two types: boot time and run time. Enable and disable are the boot time options. These options control a service at boot time. Start, stop, restart, and reload are the run-time options. These options control a service on a running system in the current session.

Both options are independent. They affect only the states to which they belong. If a service is enabled at boot time, whether you keep it running or stop it after the booting process, it will automatically start at the next system reboot. Vice versa, if a service is disabled at boot time, whether you start it after the booting process or not, it will be stopped automatically during the following booting process.

A service always starts under a unique ID. This ID is known as PID. Linux uses it to track, monitor and control the service. When we stop a service, the system deletes its PID. It never uses the PID of a stopped service to start that service again. If we start that service again, it will use a new unique PID to start the service. When we restart a service, the system stops the running service and starts a new service under the new PID. In simple words, a restart action is the combination of the stop and start actions. In the reload action, the system rereads the service's unit configuration file. Since it does not stop the running service, the service's PID does not change.

Making changes permanently

Any changes made through the start, stop, restart, and reload actions are not persistent after the system reboot. To make a change permanent, we must perform the related boot-time action. For example, if we want to stop a boot time-enabled service permanently, we must disable it at the boot time.

The systemctl command

The systemctl command manages systemd services. It uses the following syntax.

#systemctl [action] [name of service]

The following table lists the commands to perform the above-explained actions.

Action/operation Command
Start#systemctl start [name-of-service].service
Stop#systemctl stop [name-of-service].service
Restart#systemctl restart [name-of-service].service
Reload#systemctl reload [name-of-service].service
Enable#systemctl enable [name-of-service].service
Disable#systemctl disable [name-of-service].service

Checking the state of a service

To check the current state of a service, we can use the following commands.

Command Description
#systemctl status [name-of-service].service Detailed state of the service. It includes both types: run-time and boot-time.
#systemctl is-active [name-of-service].service Display only the run time state of the command
#systemctl is-enabled [name-of-service].service Display only the boot time state of the command

This tutorial is the last part of the tutorial series Systemd service in Linux. Other parts of this series are the following.

Chapter 01  Differences between SysVinit, Upstart and Systemd
Chapter 02  Systemd Units Explained with Types and States
Chapter 03  Systemd Unit Configuration Files Explained
Chapter 04  Systemd Target Units Explained

Examples

The following command displays states of the SSH service.

#systemctl status sshd.service

state of the SSH service

The following command displays the boot-time state of the SSH service.

#systemctl is-enabled sshd.service

boot time state

The following command displays the run-time state of the SSH service.

#systemctl is-active sshd.service

The following image shows the output of the above command before and after activating and deactivating the SSH service.

both states of SSH service

The following command stops the SSH service.

#systemctl stop sshd.service

SSH stopped

The following command starts the SSH service.

#systemctl start sshd.service

starting the SSH service

The following command restarts SSH service.

#systemctl restart sshd.service

restarting the SSH service

The above output verifies when we restart a service, the PID of the service changes.

The following command reloads the SSH service.

#systemctl reload sshd.service

reloading a service without restart

The above output verifies when we reload a service, the PID of the service does not change.

The following command enables the SSH service at boot time.

#systemctl enable sshd.service

enabling the SSH service

The following command disables the SSH service during boot time.

#systemctl disable sshd.service

disabling the SSH service at boot time

Conclusion

The systemctl command allows us to start, stop, restart, reload, enable and disable a service or process. Enable and disable are the boot time actions. If we enable a service at boot time, the boot process starts the service during the booting process. If disable it, the boot process does not start at boot time.

ComputerNetworkingNotes Linux Tutorials Using the systemctl command to manage Systemd services

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com