How to view running services with systemctl list-units
How to View Running Services with systemctl list-units
Table of Contents
1. [Introduction](#introduction)
2. [Prerequisites](#prerequisites)
3. [Understanding systemd and systemctl](#understanding-systemd-and-systemctl)
4. [Basic systemctl list-units Command](#basic-systemctl-list-units-command)
5. [Filtering and Customizing Output](#filtering-and-customizing-output)
6. [Practical Examples and Use Cases](#practical-examples-and-use-cases)
7. [Advanced Options and Parameters](#advanced-options-and-parameters)
8. [Monitoring Service States](#monitoring-service-states)
9. [Troubleshooting Common Issues](#troubleshooting-common-issues)
10. [Best Practices and Professional Tips](#best-practices-and-professional-tips)
11. [Alternative Methods](#alternative-methods)
12. [Conclusion](#conclusion)
Introduction
System administrators and Linux users frequently need to monitor running services to ensure system stability, troubleshoot issues, and maintain optimal performance. The `systemctl list-units` command is a powerful tool within the systemd ecosystem that provides comprehensive information about system services, their current states, and operational status.
This comprehensive guide will teach you how to effectively use `systemctl list-units` to view running services, understand service states, filter output for specific needs, and implement best practices for system monitoring. Whether you're a beginner learning Linux system administration or an experienced professional seeking to refine your skills, this article provides detailed instructions, practical examples, and expert insights.
By the end of this guide, you'll master the art of service monitoring using systemctl, understand various filtering options, and be equipped with troubleshooting techniques to resolve common issues encountered when working with systemd services.
Prerequisites
Before diving into the detailed instructions, ensure you have the following prerequisites:
System Requirements
- Linux Distribution: A modern Linux distribution using systemd (Ubuntu 16.04+, CentOS 7+, Debian 8+, Fedora, openSUSE, Arch Linux)
- systemd Version: systemd version 219 or later (most modern distributions meet this requirement)
- Terminal Access: Command-line interface access to your Linux system
User Permissions
- Regular User: Can view most service information
- Root/Sudo Access: Required for viewing certain system-level services and detailed information
- Understanding: Basic familiarity with Linux command-line interface
Verification Steps
To verify your system meets the requirements, run these commands:
```bash
Check systemd version
systemctl --version
Verify systemctl is available
which systemctl
Test basic functionality
systemctl list-units --type=service --state=running | head -5
```
Understanding systemd and systemctl
What is systemd?
systemd is a system and service manager for Linux operating systems that has become the standard init system for most modern Linux distributions. It manages system services, handles system startup, and provides various system management capabilities.
Key systemd Concepts
Units: The basic building blocks of systemd, representing system resources such as services, devices, mount points, and timers.
Service Units: Specific types of units that manage system services and daemons.
Unit States: Describe the current operational status of units (active, inactive, failed, etc.).
systemctl Command Overview
`systemctl` is the primary command-line tool for interacting with systemd. It allows you to:
- Start, stop, and restart services
- Enable and disable services
- View service status and information
- List and filter system units
- Monitor system state
Basic systemctl list-units Command
Standard Syntax
The basic syntax for the `systemctl list-units` command is:
```bash
systemctl list-units [OPTIONS] [PATTERN...]
```
Default Behavior
When executed without any options, `systemctl list-units` displays all currently loaded units:
```bash
systemctl list-units
```
This command produces output similar to:
```
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point
sys-devices-platform-serial8250-tty-ttyS0.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS0
sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS1
...
NetworkManager.service loaded active running Network Manager
ssh.service loaded active running OpenBSD Secure Shell server
systemd-journald.service loaded active running Journal Service
```
Understanding the Output Columns
UNIT: The name of the systemd unit
LOAD: Whether the unit definition was properly loaded
ACTIVE: The high-level unit activation state
SUB: The low-level unit activation state
DESCRIPTION: Human-readable description of the unit
Filtering and Customizing Output
Filtering by Unit Type
To view only service units, use the `--type` parameter:
```bash
View only services
systemctl list-units --type=service
View only running services
systemctl list-units --type=service --state=running
View only failed services
systemctl list-units --type=service --state=failed
```
Common Unit Types
```bash
Different unit types you can filter by
systemctl list-units --type=service # Services
systemctl list-units --type=socket # Socket units
systemctl list-units --type=target # Target units
systemctl list-units --type=timer # Timer units
systemctl list-units --type=mount # Mount units
systemctl list-units --type=device # Device units
```
State Filtering Options
```bash
Active units (default)
systemctl list-units --state=active
Running services specifically
systemctl list-units --type=service --state=running
Failed units
systemctl list-units --state=failed
Inactive units
systemctl list-units --state=inactive
All units (including inactive)
systemctl list-units --all
```
Available Unit States
- active: Unit is active and running
- inactive: Unit is not active
- failed: Unit failed to start or crashed
- activating: Unit is in the process of starting
- deactivating: Unit is in the process of stopping
Practical Examples and Use Cases
Example 1: Monitoring Web Server Services
```bash
Check if web servers are running
systemctl list-units --type=service --state=running | grep -E "(apache|nginx|httpd)"
Alternative approach with pattern matching
systemctl list-units --type=service "apache" "nginx" "httpd*"
```
Example 2: Database Service Monitoring
```bash
Monitor database services
systemctl list-units --type=service --state=running | grep -E "(mysql|postgresql|mariadb|mongodb)"
Check specific database service status
systemctl list-units --type=service "mysql" "postgresql"
```
Example 3: System Service Health Check
```bash
Quick health check - list failed services
systemctl list-units --state=failed
Comprehensive service overview
systemctl list-units --type=service --all | grep -E "(failed|inactive)"
```
Example 4: Network Service Monitoring
```bash
Monitor network-related services
systemctl list-units --type=service --state=running | grep -E "(network|ssh|firewall)"
Check specific network services
systemctl list-units --type=service "NetworkManager" "ssh" "ufw*"
```
Example 5: Custom Filtering with Output Formatting
```bash
Simplified output showing only unit names and states
systemctl list-units --type=service --state=running --no-legend --plain | awk '{print $1, $3}'
Count running services
systemctl list-units --type=service --state=running --no-legend | wc -l
```
Advanced Options and Parameters
Output Control Options
```bash
Remove header and footer
systemctl list-units --type=service --no-legend
Plain output (no colors or formatting)
systemctl list-units --type=service --plain
Full output (don't truncate long lines)
systemctl list-units --type=service --full
Quiet output
systemctl list-units --type=service --quiet
```
Combining Multiple Filters
```bash
Multiple unit types
systemctl list-units --type=service,socket --state=running
Pattern matching with wildcards
systemctl list-units "network" "ssh"
Complex filtering example
systemctl list-units --type=service --state=active,running --full --no-legend
```
Machine-Readable Output
```bash
JSON output for scripting
systemctl list-units --type=service --output=json
Show properties for scripting
systemctl list-units --type=service --show-types --no-pager
```
Monitoring Service States
Understanding Service Sub-States
Services can have various sub-states that provide more detailed information:
```bash
View detailed service states
systemctl list-units --type=service --all
Common sub-states:
- running: Service is actively running
- exited: Service completed successfully
- failed: Service failed to start
- dead: Service is not running
- start-pre: Service is preparing to start
- start: Service is starting
- start-post: Service is completing startup
- reload: Service is reloading configuration
- stop: Service is stopping
- stop-post: Service is completing shutdown
```
Real-Time Monitoring
```bash
Watch services in real-time (updates every 2 seconds)
watch -n 2 'systemctl list-units --type=service --state=running'
Monitor specific services
watch -n 1 'systemctl list-units --type=service "apache" "nginx" "mysql*"'
Monitor failed services
watch -n 5 'systemctl list-units --state=failed'
```
Creating Monitoring Scripts
```bash
#!/bin/bash
Simple service monitoring script
echo "=== Service Health Check ==="
echo "Date: $(date)"
echo
echo "Running Services:"
systemctl list-units --type=service --state=running --no-legend | wc -l
echo
echo "Failed Services:"
FAILED=$(systemctl list-units --type=service --state=failed --no-legend)
if [ -z "$FAILED" ]; then
echo "No failed services"
else
echo "$FAILED"
fi
echo
echo "Critical Services Status:"
for service in ssh nginx apache2 mysql postgresql; do
if systemctl is-active --quiet "$service" 2>/dev/null; then
echo "$service: RUNNING"
else
echo "$service: NOT RUNNING"
fi
done
```
Troubleshooting Common Issues
Issue 1: Permission Denied Errors
Problem: Cannot view certain system services or get "Permission denied" errors.
Solution:
```bash
Run with sudo for full system access
sudo systemctl list-units --type=service
Check if you're in the systemd-journal group
groups $USER | grep systemd-journal
Add user to systemd-journal group if needed
sudo usermod -a -G systemd-journal $USER
```
Issue 2: Command Not Found
Problem: `systemctl` command is not available.
Solution:
```bash
Check if systemd is installed
which systemd
Verify system is using systemd
ps -p 1 -o comm=
Install systemd if missing (distribution-specific)
Ubuntu/Debian:
sudo apt-get install systemd
CentOS/RHEL:
sudo yum install systemd
```
Issue 3: No Output or Empty Results
Problem: `systemctl list-units` returns no results or unexpected empty output.
Solution:
```bash
Check systemd status
systemctl status
Verify systemd is running
systemctl is-system-running
Check for specific issues
journalctl -u systemd --since "1 hour ago"
Force reload systemd configuration
sudo systemctl daemon-reload
```
Issue 4: Truncated Output
Problem: Service names or descriptions are cut off in the output.
Solution:
```bash
Use --full option to show complete information
systemctl list-units --type=service --full
Adjust terminal width or use less pager
systemctl list-units --type=service --no-pager
Export to file for full viewing
systemctl list-units --type=service --full > services.txt
```
Issue 5: Slow Command Execution
Problem: `systemctl list-units` takes a long time to execute.
Solution:
```bash
Use specific filters to reduce processing
systemctl list-units --type=service --state=running
Avoid --all option unless necessary
systemctl list-units --type=service
Check system load and memory
top
free -h
Check for systemd issues
journalctl -u systemd --since "10 minutes ago"
```
Best Practices and Professional Tips
Efficient Service Monitoring
1. Use Specific Filters: Always filter by type and state when possible to reduce output and improve performance.
```bash
Good: Specific filtering
systemctl list-units --type=service --state=running
Less efficient: No filtering
systemctl list-units --all
```
2. Create Aliases: Set up convenient aliases for common operations.
```bash
Add to ~/.bashrc or ~/.bash_aliases
alias services='systemctl list-units --type=service'
alias running-services='systemctl list-units --type=service --state=running'
alias failed-services='systemctl list-units --type=service --state=failed'
alias service-count='systemctl list-units --type=service --state=running --no-legend | wc -l'
```
3. Regular Health Checks: Implement regular service monitoring routines.
```bash
Daily health check script
#!/bin/bash
echo "Daily Service Health Report - $(date)" >> /var/log/service-health.log
systemctl list-units --type=service --state=failed >> /var/log/service-health.log 2>&1
echo "---" >> /var/log/service-health.log
```
Scripting and Automation
1. Parse Output Safely: Use appropriate tools for parsing systemctl output.
```bash
Good: Using --no-legend for scripting
systemctl list-units --type=service --state=running --no-legend | while read unit load active sub description; do
echo "Service: $unit is $active"
done
Better: Using systemctl show for detailed information
systemctl show --property=ActiveState,SubState nginx.service
```
2. Error Handling: Always include proper error handling in scripts.
```bash
#!/bin/bash
if ! command -v systemctl &> /dev/null; then
echo "Error: systemctl not found. This system may not use systemd."
exit 1
fi
if ! systemctl list-units --type=service --state=running &> /dev/null; then
echo "Error: Unable to list services. Check permissions."
exit 1
fi
```
Performance Optimization
1. Limit Output: Use appropriate filtering to reduce processing time.
```bash
Efficient: Target specific services
systemctl list-units --type=service "apache" "nginx"
Less efficient: Filter after output
systemctl list-units --type=service | grep apache
```
2. Use Machine-Readable Formats: For automated processing, use structured output.
```bash
JSON output for programmatic processing
systemctl list-units --type=service --output=json-pretty > services.json
```
Security Considerations
1. Principle of Least Privilege: Don't run with unnecessary privileges.
```bash
Check what you can access as regular user first
systemctl list-units --type=service --state=running
Only use sudo when necessary
sudo systemctl list-units --all --type=service
```
2. Log Monitoring: Keep track of service monitoring activities.
```bash
Log service checks
echo "$(date): Service check performed by $USER" >> /var/log/service-monitoring.log
systemctl list-units --type=service --state=failed >> /var/log/service-monitoring.log
```
Alternative Methods
Using systemctl status
For detailed information about specific services:
```bash
Detailed status of specific service
systemctl status nginx.service
Brief status check
systemctl is-active nginx.service
systemctl is-enabled nginx.service
systemctl is-failed nginx.service
```
Using ps Command
Traditional process monitoring:
```bash
View running processes
ps aux | grep -E "(apache|nginx|mysql)"
Process tree view
pstree -p
```
Using service Command (Legacy)
On systems with SysV init compatibility:
```bash
List services (where available)
service --status-all
Check specific service
service nginx status
```
Using journalctl for Service Logs
Monitor service activity through logs:
```bash
View service logs
journalctl -u nginx.service
Follow service logs in real-time
journalctl -u nginx.service -f
Recent service activity
journalctl -u nginx.service --since "1 hour ago"
```
Third-Party Monitoring Tools
Consider these tools for advanced monitoring:
1. htop: Enhanced process viewer
2. netdata: Real-time performance monitoring
3. Prometheus + Grafana: Comprehensive monitoring solution
4. Nagios: Enterprise monitoring platform
Conclusion
Mastering the `systemctl list-units` command is essential for effective Linux system administration. This comprehensive guide has covered everything from basic usage to advanced filtering techniques, troubleshooting common issues, and implementing best practices for service monitoring.
Key Takeaways
1. Basic Usage: `systemctl list-units --type=service` provides a clear view of all service units
2. Filtering: Use `--state` and `--type` parameters to focus on specific services
3. Real-time Monitoring: Combine with `watch` command for continuous monitoring
4. Scripting: Leverage `--no-legend` and `--plain` options for automated processing
5. Troubleshooting: Understanding common issues helps maintain system reliability
Next Steps
To further enhance your system administration skills:
1. Explore systemctl: Learn additional systemctl commands like `start`, `stop`, `enable`, and `disable`
2. Study systemd Units: Understand different unit types and their configurations
3. Implement Monitoring: Create automated monitoring scripts for your environment
4. Learn journalctl: Master log analysis for comprehensive system troubleshooting
5. Practice: Regular use of these commands will build muscle memory and expertise
Final Recommendations
- Always test commands in a safe environment before using them in production
- Keep your system updated to benefit from the latest systemd improvements
- Document your monitoring procedures for team knowledge sharing
- Consider implementing centralized logging and monitoring solutions for complex environments
By following the guidelines and techniques outlined in this comprehensive guide, you'll be well-equipped to effectively monitor and manage services using `systemctl list-units`, contributing to more stable and reliable Linux systems. Remember that consistent practice and continuous learning are key to mastering these essential system administration skills.