Complete Guide to systemctl list-units for Linux Services

Master systemctl list-units command to monitor and manage systemd services, sockets, timers, and other units in Linux distributions effectively.

Complete Guide to Viewing Running Services with systemctl list-units

Introduction

The systemctl list-units command is a fundamental tool in systemd-based Linux distributions for monitoring and managing system services. This command provides comprehensive information about all active units in the systemd ecosystem, including services, sockets, timers, and other unit types. Understanding how to effectively use this command is crucial for system administrators, developers, and anyone managing Linux systems.

Understanding systemd and Units

What is systemd

systemd is a system and service manager for Linux operating systems. It serves as the init system (PID 1) and provides a comprehensive suite of tools for managing system services, processes, and resources. systemd introduces the concept of "units" which are the basic building blocks for system management.

Types of Units

systemd manages various types of units, each serving different purposes:

| Unit Type | Extension | Description | Example | |-----------|-----------|-------------|---------| | Service | .service | System services and daemons | nginx.service, ssh.service | | Socket | .socket | Network sockets and IPC | docker.socket, systemd-journald.socket | | Timer | .timer | Scheduled tasks (cron-like) | logrotate.timer, apt-daily.timer | | Mount | .mount | File system mount points | home.mount, boot.mount | | Device | .device | Hardware devices | dev-sda1.device | | Target | .target | Groups of units | multi-user.target, graphical.target | | Path | .path | File system path monitoring | systemd-ask-password-console.path | | Slice | .slice | Resource management groups | user.slice, system.slice | | Scope | .scope | Process groups | session-1.scope | | Swap | .swap | Swap file or partition | dev-sda2.swap |

Basic Syntax and Usage

Command Structure

`bash systemctl list-units [OPTIONS] [PATTERN...] `

Basic Command

The simplest form of the command displays all currently loaded and active units:

`bash systemctl list-units `

This command outputs a table with the following columns:

| Column | Description | |--------|-------------| | UNIT | The name of the unit | | LOAD | Whether the unit definition was loaded | | ACTIVE | High-level unit activation state | | SUB | Low-level unit activation state | | DESCRIPTION | Short description of the unit |

Example Output

` UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point sys-devices-pci0000:00-0000:00:02.0-drm-card0.device loaded active plugged /sys/devices/pci0000:00/0000:00:02.0/drm/card0 sys-devices-platform-serial8250-tty-ttyS0.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS0 NetworkManager.service loaded active running Network Manager accounts-daemon.service loaded active running Accounts Service acpid.service loaded active running ACPI event daemon `

Command Options and Flags

State-based Filtering

#### --state Option

The --state option allows filtering units based on their current state:

`bash systemctl list-units --state=active systemctl list-units --state=failed systemctl list-units --state=inactive `

Available States:

| State | Description | |-------|-------------| | active | Unit is active and running | | inactive | Unit is not active | | activating | Unit is in the process of being activated | | deactivating | Unit is in the process of being deactivated | | failed | Unit failed to activate | | reloading | Unit is reloading its configuration | | maintenance | Unit is in maintenance mode |

#### Multiple States

You can specify multiple states separated by commas:

`bash systemctl list-units --state=active,failed `

Type-based Filtering

#### --type Option

Filter units by their type using the --type option:

`bash systemctl list-units --type=service systemctl list-units --type=socket systemctl list-units --type=timer `

Example: List only running services

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

#### Multiple Types

Specify multiple unit types:

`bash systemctl list-units --type=service,socket,timer `

Display Options

#### --all Option

By default, systemctl list-units only shows loaded units. Use --all to include inactive units:

`bash systemctl list-units --all `

This is particularly useful for troubleshooting as it shows units that failed to load or are currently inactive.

#### --no-pager Option

Disable pager output for scripting or when you want direct output:

`bash systemctl list-units --no-pager `

#### --no-legend Option

Remove the header and footer information:

`bash systemctl list-units --no-legend `

#### --plain Option

Combine multiple formatting options for clean output:

`bash systemctl list-units --plain --no-pager --no-legend `

Output Format Options

#### --output Option

Control the output format:

`bash systemctl list-units --output=json systemctl list-units --output=json-pretty `

Practical Examples and Use Cases

System Administration Tasks

#### Monitor System Health

Check for failed services:

`bash systemctl list-units --state=failed `

Expected Output: ` UNIT LOAD ACTIVE SUB DESCRIPTION example-service.service loaded failed failed Example Service `

#### List All Running Services

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

#### Check Network-Related Services

`bash systemctl list-units --type=service | grep -i network `

#### Monitor Timer Jobs

View scheduled tasks managed by systemd:

`bash systemctl list-units --type=timer `

Example Output: ` UNIT LOAD ACTIVE SUB DESCRIPTION apt-daily-upgrade.timer loaded active waiting Daily apt upgrade and clean activities apt-daily.timer loaded active waiting Daily apt download activities logrotate.timer loaded active waiting Daily rotation of log files `

Advanced Filtering

#### Pattern Matching

Use patterns to filter specific units:

`bash systemctl list-units 'ssh*' systemctl list-units 'network' systemctl list-units '*.timer' `

#### Combining Options

List all failed services and sockets:

`bash systemctl list-units --type=service,socket --state=failed --all `

Scripting and Automation

#### Extract Unit Names Only

`bash systemctl list-units --type=service --state=running --no-legend --plain | awk '{print $1}' `

#### Count Running Services

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

#### Service Status Check Script

`bash #!/bin/bash

Check if critical services are running

critical_services=("ssh" "nginx" "mysql" "docker")

for service in "${critical_services[@]}"; do if systemctl list-units --type=service --state=running | grep -q "${service}.service"; then echo "${service}: Running" else echo "${service}: Not running" fi done `

Understanding Unit States

Load States

| Load State | Description | |------------|-------------| | loaded | Unit definition was successfully loaded | | error | Unit definition failed to load | | masked | Unit is masked and cannot be started | | not-found | Unit definition file was not found |

Active States

| Active State | Description | |--------------|-------------| | active | Unit is active (started, bound, plugged in) | | inactive | Unit is not active | | activating | Unit is currently being activated | | deactivating | Unit is currently being deactivated | | failed | Unit failed in some way | | reloading | Unit is reloading its configuration |

Sub States (Service Units)

| Sub State | Description | |-----------|-------------| | running | Service is currently running | | exited | Service successfully completed and exited | | failed | Service failed to start or crashed | | dead | Service is not running | | start-pre | Service is preparing to start | | start | Service is starting | | start-post | Service has started and is running post-start commands | | reload | Service is reloading | | stop | Service is stopping | | stop-sigterm | Service received SIGTERM | | stop-sigkill | Service received SIGKILL | | stop-post | Service is running post-stop commands | | final-sigterm | Service is in final termination phase | | final-sigkill | Service is being force-killed | | auto-restart | Service will be automatically restarted |

Troubleshooting with list-units

Identifying Failed Services

When services fail, they appear in the failed state:

`bash systemctl list-units --state=failed `

Follow-up Commands:

`bash

Get detailed status of failed service

systemctl status failed-service.service

View logs for failed service

journalctl -u failed-service.service

Check service dependencies

systemctl list-dependencies failed-service.service `

Monitoring Service Dependencies

List units that depend on a specific service:

`bash systemctl list-dependencies --reverse ssh.service `

Checking Unit File Issues

List units with load errors:

`bash systemctl list-units --state=error `

Performance Considerations

Output Optimization

For systems with many units, consider using filters to reduce output:

`bash

Instead of listing all units

systemctl list-units

Filter to specific types

systemctl list-units --type=service `

Memory Usage

The --all option can show many inactive units, which may impact performance on systems with limited resources:

`bash

More resource-intensive

systemctl list-units --all

More efficient for basic monitoring

systemctl list-units --state=active `

Integration with Other Commands

Combining with grep

`bash

Find database-related services

systemctl list-units --type=service | grep -i database

Find services containing specific keywords

systemctl list-units | grep -E "(network|wifi|ethernet)" `

Using with awk for Data Extraction

`bash

Extract service names and their states

systemctl list-units --type=service --no-legend | awk '{print $1, $3}'

Count services by state

systemctl list-units --type=service --no-legend | awk '{print $3}' | sort | uniq -c `

Integration with Monitoring Scripts

`bash #!/bin/bash

System health check script

echo "=== Failed Services ===" systemctl list-units --state=failed --no-legend

echo -e "\n=== Service Count by State ===" systemctl list-units --type=service --no-legend | awk '{print $3}' | sort | uniq -c

echo -e "\n=== Critical Services Status ===" for service in ssh nginx apache2 mysql postgresql docker; do if systemctl list-units --type=service | grep -q "${service}.service"; then state=$(systemctl list-units --type=service | grep "${service}.service" | awk '{print $3}') echo "${service}: ${state}" fi done `

Best Practices

Regular Monitoring

Establish regular monitoring routines:

1. Daily Health Checks: `bash systemctl list-units --state=failed `

2. Weekly Service Review: `bash systemctl list-units --type=service --all | grep inactive `

3. Monthly Comprehensive Audit: `bash systemctl list-units --all > /var/log/systemctl-audit-$(date +%Y%m%d).log `

Documentation and Logging

Keep records of system state changes:

`bash

Create system state snapshot

systemctl list-units --all > system-state-$(date +%Y%m%d-%H%M).txt `

Automation Guidelines

When using in scripts:

1. Always use --no-pager for non-interactive execution 2. Use --no-legend when parsing output 3. Include error handling for failed commands 4. Consider using --plain for cleaner parsing

`bash #!/bin/bash

Robust service checking function

check_service_status() { local service_name="$1" local status if status=$(systemctl list-units --type=service --no-pager --no-legend --plain | grep "^${service_name}.service"); then echo "Service found: $status" return 0 else echo "Service not found or not loaded: $service_name" return 1 fi } `

Conclusion

The systemctl list-units command is an essential tool for managing and monitoring systemd-based Linux systems. Its flexibility in filtering by state, type, and pattern matching makes it invaluable for system administration tasks ranging from basic health checks to complex automation scripts.

Key takeaways for effective usage:

1. Understand Unit Types: Different unit types serve different purposes in the systemd ecosystem 2. Use Appropriate Filters: Combine --type and --state options to focus on relevant information 3. Leverage Output Options: Use --no-pager, --no-legend, and --plain for scripting 4. Monitor Regularly: Establish routines for checking system health using failed state filters 5. Combine with Other Tools: Integrate with grep, awk, and custom scripts for comprehensive monitoring

Regular use of systemctl list-units with its various options will provide deep insights into system behavior and help maintain stable, well-monitored Linux environments. Whether you're troubleshooting issues, monitoring system health, or automating administrative tasks, mastering this command is fundamental to effective systemd management.

Tags

  • Linux
  • service management
  • system-administration
  • systemctl
  • systemd

Related Articles

Popular Technical Articles & Tutorials

Explore our comprehensive collection of technical articles, programming tutorials, and IT guides written by industry experts:

Browse all 8+ technical articles | Read our IT blog

Complete Guide to systemctl list-units for Linux Services