How to Use the Command 'systemctl' (with Examples)

How to Use the Command 'systemctl' (with Examples)

The systemctl command is an integral part of the systemd system and service manager, responsible for controlling the system’s service and unit states. It facilitates the management of the Linux initialization process, starting from booting up the systems to running the desired services or units. Whether an end-user intends to observe the running status of services, manage them, or configure startup settings, systemctl is indispensable for these operations. This article delves into several practical applications of systemctl, showcasing its versatile functionality with detailed examples.

Use case 1: Show All Running Services

Code:

systemctl status

Motivation:
When managing a Linux server, it’s crucial to understand which services are operational at any moment. By listing all running services, system administrators can quickly verify if essential services, such as web servers or database servers, are active. This helps in monitoring system health and diagnosing issues promptly.

Explanation:
The systemctl status command provides a comprehensive overview of the system’s current state, displaying which of the services are running. It presents a summary of all active services, along with information such as main process IDs, CPU usage, and more.

Example Output:

● example.service - Example Service
   Loaded: loaded (/usr/lib/systemd/system/example.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2023-11-20 10:22:51 UTC; 10min ago
     Docs: http://example.com/docs
 Main PID: 1234 (example)
    Tasks: 5 (limit: 9546)
   Memory: 12.5M
   CGroup: /system.slice/example.service
           └─1234 /usr/bin/example

Use case 2: List Failed Units

Code:

systemctl --failed

Motivation:
Identifying failed services or units quickly is crucial for system reliability and troubleshooting. Detecting which services failed allows administrators to address problems promptly to minimize downtime and maintain service availability, ensuring that critical applications remain operable.

Explanation:
The argument --failed filters out services or units that have encountered issues and have not started as expected. This command focuses solely on problem areas, helping the administrator pinpoint specific failures for further investigation.

Example Output:

UNIT                 LOAD   ACTIVE SUB    DESCRIPTION
example-fail.service loaded failed failed An Example Failing Service

Use case 3: Start/Stop/Restart/Reload/Show the Status of a Service

Code:

systemctl start|stop|restart|reload|status unit

Motivation:
Managing the lifecycle of a service efficiently is a common task for Linux administrators. The ability to start, stop, restart, reload configurations of, or check the status of a specific service unit provides robust control over service operations. This is crucial when deploying changes, handling service upgrades, or performing routine maintenance.

Explanation:

  • start: Initiates a service, making it active.
  • stop: Halts an active service.
  • restart: Stops and then starts a service, useful when changes requiring a full reload are made.
  • reload: Reloads the configuration of a service without a full restart, if supported by the service.
  • status: Displays the current status, running state, and log tail of a service unit.

Example Output for systemctl status example.service:

● example.service - Example Service
   Loaded: loaded (/usr/lib/systemd/system/example.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2023-11-20 10:22:51 UTC; 15min ago
 Main PID: 1234 (example)
    Tasks: 5 (limit: 9546)
   CGroup: /system.slice/example.service
           └─1234 /usr/bin/example

Use case 4: Enable/Disable a Unit to Be Started on Bootup

Code:

systemctl enable|disable unit

Motivation:
Controlling whether a service starts automatically when the system boots up is a critical configuration. By enabling essential services to start on boot, consistency and reliability are maintained in regular operations. Conversely, disabling non-essential or problematic services during bootup can prevent resource hogging and potential conflicts.

Explanation:

  • enable: Configures the unit to start automatically upon system boot-up by creating necessary symlinks.
  • disable: Removes symlinks for the unit, preventing it from starting automatically during bootup.

Example Output for systemctl enable example.service:

Created symlink /etc/systemd/system/multi-user.target.wants/example.service → /usr/lib/systemd/system/example.service.

Use case 5: Reload Systemd to Scan for New or Changed Units

Code:

systemctl daemon-reload

Motivation:
After creating, modifying, or deleting service unit files, a reload of the systemd configuration is necessary for these changes to take effect. This command reads the state of system units without impacting currently running services, a non-disruptive method to apply updates in configuration.

Explanation:

  • daemon-reload: Instructs systemd to reload its configuration files, recognizing new or modified units without stopping services. This is typically used after changes in service definition files.

Example Output:

# No direct output; success indicates the configuration has been reloaded.

Use case 6: Check if a Unit Is Active/Enabled/Failed

Code:

systemctl is-active|is-enabled|is-failed unit

Motivation:
Checking the state of specific units allows administrators to quickly verify the operation status, startup behavior, and whether they have encountered failures. These checks are useful in scripts or automation tasks where conditional actions may depend on the status of a service.

Explanation:

  • is-active: Returns whether a service is currently running.
  • is-enabled: Indicates if a service is set to start at boot.
  • is-failed: Provides information on whether the service has failed.

Example Output for systemctl is-active example.service:

active

Use case 7: List All Service/Socket/Automount Units Filtering by Running/Failed State

Code:

systemctl list-units --type=service|socket|automount --state=failed|running

Motivation:
When monitoring system health, seeing specific units filtered by their running or failed state is vital. A filtered list helps administrators focus on operable or problematic parts of the system, making it easier to engage in both casual monitoring and in-depth diagnostics.

Explanation:

  • --type=service|socket|automount: Specifies the unit type, allowing for focused searches.
  • --state=failed|running: Filters units based on their state, showing only those that are running or have failed.

Example Output for systemctl list-units --type=service --state=running:

UNIT                  LOAD   ACTIVE SUB     DESCRIPTION
example.service       loaded active running An Example Service

Use case 8: Show the Contents & Absolute Path of a Unit File

Code:

systemctl cat unit

Motivation:
Inspecting the configuration of a service unit can be necessary for troubleshooting, auditing, or education. By displaying the contents and path, administrators gain insight into service parameters and systemd configurations.

Explanation:

  • cat: Used here to read and display the contents of the specified unit file along with its absolute path.

Example Output:

# /usr/lib/systemd/system/example.service
[Unit]
Description=An Example Service

[Service]
ExecStart=/usr/bin/example

[Install]
WantedBy=multi-user.target

Conclusion:

The systemctl command is a versatile tool that offers comprehensive control and insight into managing Linux systems through the systemd service manager. Understanding these use cases, from listing services to enabling them on boot, allows system administrators to maintain system operability, efficiency, and reliability. Each use case is a part of the broader scope of managing the Linux lifecycle, highlighting the importance and utility of systemctl in various administrative tasks.

Related Posts

Utilizing the Command 'dep' for PHP Application Deployment (with Examples)

Utilizing the Command 'dep' for PHP Application Deployment (with Examples)

The dep command is a powerful tool for deploying PHP applications, specifically designed to streamline and automate the deployment process using Deployer.

Read More
How to use the command 'qm move_disk' (with examples)

How to use the command 'qm move_disk' (with examples)

The qm move_disk command, an alias of qm disk move, is a tool used in Proxmox VE (Virtual Environment) for relocating virtual machine disks.

Read More
How to Use the Command 'setenforce' (with Examples)

How to Use the Command 'setenforce' (with Examples)

The setenforce command is a powerful tool for system administrators working with Linux servers.

Read More