How to List All Running Services From Systemctl?

Published July 9, 2024

Problem: Viewing Active Services in Linux

Managing services on a Linux system is a task for administrators and users. Systemctl, the main command for controlling systemd, offers a way to manage and monitor these services. Getting an overview of all running services can be difficult without knowing the right commands and options to use.

Listing All Running Services with Systemctl

Basic Command to List Services

To list all services managed by systemd, use this command:

systemctl list-units --type=service

This command shows a table with information about each service unit. The output includes:

  • UNIT: The name of the service unit
  • LOAD: Whether the unit definition was loaded
  • ACTIVE: The high-level unit activation state
  • SUB: The low-level unit activation state
  • DESCRIPTION: A short description of the unit's purpose

To show only active services, add the --state=active option:

systemctl list-units --type=service --state=active

Filtering for Running Services

To list only the services that are running, use:

systemctl list-units --type=service --state=running

This command filters out inactive or failed services, showing only those running on your system.

For a more compact view, use the --no-pager option:

systemctl list-units --type=service --state=running --no-pager

This prevents the output from being paginated, which is useful when you want to pipe the results to other commands or files.

For a quick overview of running services without extra details, use:

systemctl list-units --type=service --state=running --plain --no-legend

This command removes the header and footer information, providing a clean list of running services.

To count the number of running services, pipe the output to wc:

systemctl list-units --type=service --state=running --plain --no-legend | wc -l

This command will return the total number of running services on your system.

Tip: Sort running services by memory usage

To understand which services are using the most resources, you can sort the running services by memory usage. This can help system administrators optimize system performance.

Here's a command to list running services sorted by memory usage:

systemctl list-units --type=service --state=running --no-pager | awk '{print $1}' | xargs -I {} systemctl status {} | grep Memory | sort -rnk 3 | head -n 10

This command:

  1. Lists all running services
  2. Extracts the service names
  3. Gets the status of each service
  4. Filters for the Memory usage line
  5. Sorts the results by memory usage (highest to lowest)
  6. Shows the top 10 memory-consuming services

The output will look like this:

Memory: 1.2G
Memory: 800.5M
Memory: 456.3M
Memory: 234.1M
Memory: 189.7M
Memory: 156.2M
Memory: 98.4M
Memory: 76.9M
Memory: 54.3M
Memory: 43.8M

Advanced Systemctl Commands for Service Management

Viewing Detailed Service Information

To get more information about a service, use the systemctl status command followed by the service name:

systemctl status service_name

This command provides an overview of the service, including:

  • Current state (active, inactive, failed)
  • Main PID (Process ID)
  • Memory usage
  • CPU usage
  • Start time
  • Recent log entries

Example: Viewing SSH Service Details

systemctl status sshd

The output includes the service's current status, configuration file location, and recent log entries, which help with troubleshooting.

Checking Service Status

To check the status of a service, use:

systemctl is-active service_name

This command returns "active" if the service is running, or "inactive" if it's not.

To check if a service starts at boot:

systemctl is-enabled service_name

This returns "enabled" if the service starts automatically at boot, or "disabled" if it doesn't.

You can also use the systemctl show command to get specific properties of a service:

systemctl show -p ActiveState service_name
systemctl show -p UnitFileState service_name

These commands display the current active state and whether the service is enabled.

To list all failed services:

systemctl --failed --type=service

This command helps identify services that have problems and may need attention.

Alternative Methods for Listing Services

Using PS Command

The ps command offers another way to list running services in Linux. While not as detailed as systemctl, it can give a quick view of active processes, including services.

To list all running processes, including services:

ps aux

This command shows all processes for all users in a detailed format. To filter for specific services, you can use grep:

ps aux | grep service_name

For a focused view of services, you can use:

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

This command displays the process ID, parent process ID, command, memory usage, and CPU usage, sorted by memory usage.

Compared to systemctl, ps provides:

  • Real-time process information
  • More detailed resource usage data
  • The ability to see non-systemd managed processes

However, it lacks systemd-specific information like service states and dependencies.

Tip: Custom PS Output

You can customize the ps output to show only the information you need. For example, to display only the PID, command name, and memory usage of processes, use:

ps -eo pid,comm,%mem --sort=-%mem | head

This command sorts processes by memory usage and shows the top 10 memory-consuming processes.

Using Top Command

The top command gives a dynamic, real-time view of running processes, including services. To use top:

top

This opens an interactive display showing:

  • System summary (uptime, load average, CPU usage)
  • Process list (sorted by CPU usage by default)

To focus on services, you can use:

top -c -p $(pgrep -d',' -f service_name)

This command shows only the processes related to the specified service.

Advantages of using top:

  • Real-time updates of system and process information
  • Interactive interface for sorting and filtering
  • Ability to see resource usage trends over time

Limitations include:

  • Less service-specific information compared to systemctl
  • May require more system resources for continuous monitoring

Both ps and top complement systemctl by providing different views on running services and processes, allowing for a better understanding of system activity.

Example: Monitoring Apache Web Server

To monitor the Apache web server using top, you can use:

top -c -p $(pgrep -d',' -f apache2)

This command will display real-time information about all Apache processes, including their CPU and memory usage. It's useful for tracking Apache's resource consumption during high traffic periods.