How to use the command 'systemd-analyze' (with examples)

How to use the command 'systemd-analyze' (with examples)

Systemd is a system manager and init system for Linux operating systems. systemd-analyze is a command that helps analyze and debug systemd and provides information about the boot process of units such as services, mount points, devices, and sockets. In this article, we will explore different use cases of the systemd-analyze command.

Use case 1: List all running units, ordered by the time they took to initialize

Code:

systemd-analyze blame

Motivation: This use case is helpful to identify units that are taking a long time to initialize during the boot process. By listing the units in order of their initialization time, we can understand which services or processes are causing delays and may need further investigation or optimization.

Explanation: The blame argument tells systemd-analyze to list running units ordered by the time they took to initialize.

Example output:

33.524s systemd-journal-flush.service
32.707s dev-sda1.device
22.268s dkms.service
19.796s NetworkManager-wait-online.service
18.094s systemd-udevd.service
...

Use case 2: Print a tree of the time-critical chain of units

Code:

systemd-analyze critical-chain

Motivation: Understanding the time-critical chain of units can provide insights into dependencies and bottlenecks in the boot process. It helps identify units that have long dependencies or are causing delays in other units.

Explanation: The critical-chain argument tells systemd-analyze to print a tree of the time-critical chain of units.

Example output:

The time after the unit is active or started is printed after the "@" character.
The time the unit takes to start is printed after the "+" character.

graphical.target @3.372s
└─multi-user.target @3.372s
  └─docker.service @3.332s +39ms
    └─network-online.target @3.330s
      └─NetworkManager-wait-online.service @1.533s +1.796s
        └─NetworkManager.service @1.424s +101ms
          └─dbus.service @1.393s
            └─basic.target @1.143s
              └─sockets.target @1.143s
                └─dbus.socket @1.143s
...

Use case 3: Create an SVG file showing when each system service started, highlighting the time spent on initialization

Code:

systemd-analyze plot > path/to/file.svg

Motivation: By generating an SVG file, we can visualize the boot process and easily identify the time spent on initialization for each system service. This can help in identifying any performance issues or delays that may need attention.

Explanation: The plot argument tells systemd-analyze to create an SVG file showing when each system service started. The output of the command is redirected to the specified file path.

Example output: An SVG file is generated at the specified path/to/file.svg location, which can be opened in a web browser or an SVG viewer to visualize the boot process.

Use case 4: Plot a dependency graph and convert it to an SVG file

Code:

systemd-analyze dot | dot -Tsvg > path/to/file.svg

Motivation: A visual representation of the dependency graph can help understand the relationships between different units and identify any circular or redundant dependencies.

Explanation: The dot argument is used with systemd-analyze to generate a dependency graph in the DOT format. This output is then piped to the dot -Tsvg command, which converts the DOT file to an SVG format. The resulting SVG file is saved at the specified file path.

Example output: An SVG file is generated at the specified path/to/file.svg location, which can be opened in a web browser or an SVG viewer to visualize the dependency graph.

Use case 5: Show security scores of running units

Code:

systemd-analyze security

Motivation: Security is a critical aspect of system management. By checking the security scores of running units, administrators can identify any potential vulnerabilities or security weaknesses and take appropriate actions to mitigate them.

Explanation: The security argument tells systemd-analyze to show the security scores of running units.

Example output:

UNIT                                                                              SCORE                                                                              TIME   TIME GENERATION TIME  
security.unit                                                                                  0.01                                                                                  -                       0                         148.17n $('.photographerID').on('focus', function () {
    $('.viewCart').fadeOut();
    $('.photographerID').addClass("isActive");
})

Related Posts

How to use the command Sort-Object (with examples)

How to use the command Sort-Object (with examples)

The Sort-Object command in PowerShell is used to sort objects by property values.

Read More
How to use the command xml escape (with examples)

How to use the command xml escape (with examples)

This article will guide you through the different use cases of the command xml escape.

Read More
How to use the command `gdaladdo` (with examples)

How to use the command `gdaladdo` (with examples)

gdaladdo is a command-line tool in the GDAL library that is used to build overview images of raster datasets.

Read More