Table of Contents
- Understanding Systemd Services: A Quick Primer
- Essential Diagnostic Tools
- 3.1 systemctl: The Swiss Army Knife of Systemd Management
- 3.2 journalctl: Diving into Systemd Logs
- 3.3 systemd-analyze: Profiling Boot and Service Performance
- 3.4 strace: Tracing System Calls for Debugging
- 3.5 lsof: Identifying Open Files and Resources
- 3.6 dmesg: Checking Kernel Messages
- 3.7 systemd-cgls and systemd-cgtop: Inspecting Control Groups
- Advanced Troubleshooting Workflow
- Conclusion
- References
Understanding Systemd Services: A Quick Primer
Before diving into tools, let’s clarify key systemd concepts:
- Units: The basic building blocks of systemd, representing resources to manage (e.g., services, sockets, timers, targets). A service unit (
.servicefile) defines how a service starts, runs, and stops. - Unit Files: Configuration files for units, stored in:
/lib/systemd/system/: Default units provided by the OS or packages./etc/systemd/system/: Custom or overridden units (takes precedence).
- Service Lifecycle: Services can be
active(running),inactive(stopped), orfailed(crashed/errored). Dependencies (e.g.,After=,Requires=) dictate startup order.
Essential Diagnostic Tools
3.1 systemctl: The Swiss Army Knife of Systemd Management
systemctl is the primary command for interacting with systemd. It lets you check statuses, start/stop services, modify unit files, and more.
Key Commands
| Command | Purpose |
|---|---|
systemctl status <service> | Show detailed status (active/inactive/failed, logs, PID, dependencies). |
systemctl start/stop/restart <service> | Control service state. |
systemctl enable/disable <service> | Enable/disable auto-start on boot. |
systemctl is-active <service> | Check if a service is currently running (output: active/inactive). |
systemctl is-enabled <service> | Check if a service is enabled (output: enabled/disabled/masked). |
systemctl list-units --type=service --state=failed | List all failed services. |
systemctl cat <service> | View the full unit file (useful for debugging configs). |
systemctl edit <service> | Edit the unit file (creates an override in /etc/systemd/system/). |
systemctl show <service> | Display low-level properties (e.g., ExecStart, User, Restart). |
Examples
-
Check why
nginxfailed:systemctl status nginxOutput might show: “Active: failed (Result: exit-code) since…” with a snippet of logs.
-
Verify if
sshdis enabled on boot:systemctl is-enabled sshd -
List all failed services:
systemctl list-units --type=service --state=failed
3.2 journalctl: Diving into Systemd Logs
Systemd centralizes logs in the journal, a binary database managed by systemd-journald. journalctl queries this journal, making it indispensable for debugging service failures.
Key Options
| Option | Purpose |
|---|---|
-u <service> | Filter logs for a specific service (e.g., journalctl -u nginx). |
-f | ”Follow” real-time logs (like tail -f). |
--since "YYYY-MM-DD HH:MM:SS" / --until | Filter logs by time (e.g., --since "1 hour ago"). |
-p <priority> | Filter by log priority (emerg, alert, crit, err, warning, notice, info, debug). |
-o json | Output logs in JSON for parsing (e.g., `journalctl -u nginx -o json |
-b | Show logs from the current boot (add -b -1 for the previous boot). |
Examples
-
View all logs for
mysqlfrom the last 30 minutes:journalctl -u mysql --since "30 minutes ago" -
Follow real-time errors for
apache2:journalctl -u apache2 -f -p err -
Check why a service failed on the previous boot:
journalctl -u <service> -b -1
3.3 systemd-analyze: Profiling Boot and Service Performance
systemd-analyze helps identify slow-booting services or misconfigured units that delay startup. It can also validate unit file syntax.
Key Commands
| Command | Purpose |
|---|---|
systemd-analyze | Show total boot time. |
systemd-analyze blame | List services by startup time (slowest first). |
systemd-analyze critical-chain | Visualize the critical path of boot dependencies. |
systemd-analyze verify <service>.service | Validate unit file syntax for errors. |
Examples
-
Find which services slowed down boot:
systemd-analyze blameOutput might show: “12.345s NetworkManager.service” indicating a slow network setup.
-
Check for unit file syntax errors:
systemd-analyze verify nginx.serviceCatches issues like missing
=inExecStart=/usr/bin/nginx(a common typo).
3.4 strace: Tracing System Calls for Debugging
If logs don’t reveal the root cause, strace traces system calls (e.g., open(), read(), execve()) and signals from a process. This is invaluable for debugging issues like missing files, permission errors, or failed network connections.
Key Options
| Option | Purpose |
|---|---|
-p <PID> | Attach to a running process (e.g., strace -p 1234). |
-f | Follow child processes (critical for services that fork). |
-e <syscall> | Filter specific system calls (e.g., -e open,connect). |
-o <file> | Save output to a file (e.g., strace -o nginx-strace.log -f -p <PID>). |
-u <user> | Run the command as a specific user (e.g., strace -u www-data /usr/sbin/nginx). |
Example
Debug why nodejs-app can’t start:
strace -f -e open,read,connect -o nodejs-strace.log /usr/bin/node /opt/app/server.js
Check nodejs-strace.log for ENOENT (file not found) or EACCES (permission denied) errors.
3.5 lsof: Identifying Open Files and Resources
lsof (List Open Files) identifies files, network sockets, and pipes opened by a process. It’s critical for diagnosing issues like “port already in use” or “deleted files still held open.”
Key Options
| Option | Purpose |
|---|---|
-p <PID> | List files opened by a process (e.g., lsof -p $(pidof sshd)). |
-i :<port> | Find which process is using a port (e.g., lsof -i :80). |
+L1 | List files with link count 0 (unlinked but still open, wasting disk space). |
-u <user> | List files opened by a user (e.g., lsof -u www-data). |
Examples
-
Find which process is using port 443 (conflicting with
nginx):lsof -i :443Output might show
apache2using the port, causingnginxto fail. -
Check if
mysqlis still using a deleted log file:lsof +L1 | grep mysql
3.6 dmesg: Checking Kernel Messages
The kernel ring buffer (dmesg) logs low-level system events, including hardware errors, driver issues, and process crashes. Use it to diagnose kernel-level problems affecting services.
Key Options
| Option | Purpose |
|---|---|
-T | Show human-readable timestamps. |
-w | ”Watch” for new kernel messages (like tail -f). |
-l err | Filter for errors only. |
Example
Check for kernel-level failures related to docker:
dmesg -T | grep -i docker
Output might show: “docker0: port 1(vethXXXX) entered disabled state” indicating a network issue.
3.7 systemd-cgls and systemd-cgtop: Inspecting Control Groups
Systemd uses control groups (cgroups) to manage resource limits (CPU, memory, I/O) for services. systemd-cgls and systemd-cgtop help diagnose resource starvation.
Usage
systemd-cgls: Show the cgroup hierarchy (e.g.,systemd-cgls /system.slice/nginx.service).systemd-cgtop: Real-time resource usage (CPU, memory, I/O) of cgroups (liketopfor services).
Example
Check if mongodb is hitting memory limits:
systemd-cgtop
Look for high Memory usage under /system.slice/mongodb.service.
Advanced Troubleshooting Workflow
When faced with a stubborn service failure, follow this systematic workflow:
- Check Status: Run
systemctl status <service>to get a high-level overview (active/failed, error snippets). - Review Logs: Use
journalctl -u <service> --since "10 minutes ago"to dig into service-specific logs. Look forERROR,Failed, orPermission denied. - Validate Unit File: Use
systemctl cat <service>to check for typos, thensystemd-analyze verify <service>.serviceto validate syntax. - Check Dependencies: Run
systemctl list-dependencies <service> --failedto identify failed dependencies (e.g., a missing database). - Trace System Calls: If logs are vague, use
straceto trace the service’s execution and spot missing files or blocked network calls. - Inspect Resources: Use
systemd-cgtoporlsofto check for resource limits (e.g., port conflicts, memory leaks).
Conclusion
Diagnosing systemd service failures requires a mix of tools and a systematic mindset. By mastering systemctl for status checks, journalctl for logs, strace for deep debugging, and others like lsof and dmesg, you can resolve even the most complex issues. Remember: start with the basics (status and logs), then drill down into dependencies, resources, or system calls as needed. With these tools in your toolkit, you’ll turn frustrating failures into solvable puzzles.