Using systemctl command in Linux


The systemctl command in Linux is responsible for controlling a widely adopted “systemd” init system and service manager. The init is the first process that starts when you power on your Linux system.

The systemd is a newer init system whose primary purpose is initializing, managing, and tracking system services and daemons. It replaces the traditional sys v init system and nowadays adopted by the majority of Linux distributions.

In this article, we will discuss the usage of systemctl command in Linux with some examples.

Syntax of the systemctl command

How to use systemctl on a Linux system, the syntax is given below –

systemctl [options..] command [unit..]

You can find a detailed list of options on the command’s man page.

List all the running units

A unit file contains the information of a service, socket, device, mount point, partition, swap file, a start-up target, or a group of externally created processes. To list all units in your system use the following command in your terminal –

systemctl

Now see the output of this command –

Check whether a unit or service is running or not

You can check whether a unit or service is running or not by executing the following command in your terminal –

systemctl status service-name

For example –

systemctl status mysql.service

As you can see in the image the mysql service is active and running.

How to start a service

To start a service in your system use the following command in your terminal –

sudo systemctl start service-name

Replace the service-name with yours. Once this command gets executed successfully you can verify that the service is started or not by checking its status.

How to stop a service

Now if you want to stop a service that is currently running, use the given command in your terminal –

sudo systemctl stop service-name

Again you can verify the service is stopped or not by checking its status.

Enabling or disabling a service on Linux

When you enable a service it starts automatically when you start your system next time. Now use the following command to enable a service on your system.

sudo systemctl enable service-name

Similarly, you can disable a service if you want by using –

sudo systemctl disable service-name

Check if a service is enabled or not

After enabling service in your system you can check if the service is enabled or not by using –

sudo systemctl is-enabled service-name

For example –

sudo systemctl is-enabled mysql.service

How to restart a service

Sometimes you need to restart a running service after making changes in its configuration file you can do this by using –

sudo systemctl restart service-name

You can find more information about systemctl on its manual page. Now execute the given command to see its manual page in your terminal –

man systemctl

How to analyze systemd boot process

The systemd-analyze is also a systemd utility command that helps in tracking down the process execution time on a Linux system.

You can analyze the systemd boot process by using the following command in your terminal –

systemd-analyze

As you can see in the image the total time taken by the system to boot is 1min 6.278s.

To find how much time is taken by each process while system boot, use –

systemd-analyze blame

A list of processes and in how much time they are executed will be displayed in your terminal.

Conclusion

This is how you can use the systemctl command on your Linux system. Now if you have a query then leave it in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.