To check if systemctl is "working" in Linux, you generally mean one of two things:
- Is the
systemctl command itself functional and is systemd the active init system?
- Is a specific service managed by
systemctl working correctly?
- Is the overall
systemd init system healthy and managing services as expected?
Let's cover all these aspects.
1. Checking if systemctl (the command) is functional and systemd is the init system
First, ensure your system actually uses systemd as its init system, as systemctl is the primary command-line tool for systemd.
a. Check what Init System is Running (PID 1):
The first process started by the kernel (PID 1) is typically your init system.
cat /proc/1/comm
- If it returns
systemd, then systemd is running.
- If it returns
init (for SysVinit), openrc (for OpenRC), or something else, then systemd is not your primary init system, and systemctl commands won't manage services in the usual way (though systemd might still be installed for other purposes on some systems).
b. Test the systemctl Command Itself:
Just run a basic systemctl command. If it executes without "command not found" errors or severe system errors, it's likely working.
systemctl status
This command shows the status of the systemd manager itself.
- Expected Output (if working): You should see information about
systemd (PID 1), its state (usually active (running)), uptime, memory usage, etc.
- Error Indicators:
bash: systemctl: command not found: systemctl is not in your PATH or not installed.
Failed to connect to bus: No such file or directory: Indicates a problem with the D-Bus system or systemd not running properly.
- Other permission errors or crashes.
2. Checking if a Specific Service is Working
This is the most common use case for "is systemctl working for X service?".
a. Check the Status of a Service:
systemctl status <service_name>
Replace <service_name> with the actual service you want to check (e.g., nginx, apache2, sshd, docker, mysql).
Example: systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2023-10-26 10:00:00 UTC; 1 day 5h ago
Docs: man:sshd(8)
man:ssh_config(5)
man:sshd_config(5)
Main PID: 1234 (sshd)
Tasks: 1 (limit: 4612)
Memory: 6.0M
CPU: 139ms
CGroup: /system.slice/sshd.service
└─1234 "/usr/sbin/sshd -D"
Oct 26 10:00:00 myhost systemd[1]: Started OpenSSH server daemon.
# ... (recent log entries)
Key things to look for in the output:
Loaded::
loaded (/path/to/service.service; enabled; ...): The unit file was found and is configured to start on boot (enabled).
loaded (...; disabled; ...): The unit file was found, but it won't start on boot.
not-found or masked: The unit file doesn't exist or is prevented from running.
Active:: This is the most crucial part.
active (running): The service is currently running successfully.
inactive (dead): The service is stopped.
active (exited): The service ran, completed its task, and exited successfully (common for one-shot services).
active (waiting): The service is waiting for an event or resource.
failed: The service attempted to start but failed. This indicates a problem.
activating: The service is currently in the process of starting.
Main PID:: Shows the process ID of the main process for the service.
CGroup:: Shows the control group where the service's processes are running.
- Recent Log Entries: The last few lines often provide immediate clues from the service's logs.
b. Quick Checks for Service Status:
- Is it active (running)?
systemctl is-active <service_name>
Returns active, inactive, failed, etc., and an exit code (0 for active, non-zero otherwise).
- Is it enabled (starts on boot)?
systemctl is-enabled <service_name>
Returns enabled or disabled and an exit code.
- Is it failed?
systemctl is-failed <service_name>
Returns failed or active, inactive, etc.
3. Checking Overall systemd System Health
a. List All Failed Units:
This is excellent for quickly identifying any services or other systemd units that are in a failed state.
systemctl --failed
If this command returns nothing, it means no units are currently in a failed state. If it lists units, you can then investigate them with systemctl status <unit_name> and journalctl -xeu <unit_name>.
b. View All Running Services:
systemctl list-units --type=service --state=running
This shows all services that are currently active and running.
c. Check System Boot Times (Performance):
systemd-analyze can give you insights into your system's boot process and identify potential bottlenecks.
systemd-analyze
Shows total boot time.
systemd-analyze blame
Lists all services and their individual boot times, ordered by the time they took to start.
Troubleshooting a Failed Service
If systemctl status <service_name> shows Active: failed, here's how to investigate:
-
View detailed logs for the failed service:
journalctl -xeu <service_name>
-x: Explains messages.
-e: Jumps to the end of the journal.
-u <service_name>: Filters by unit.
This is the most important step! It will usually tell you why the service failed (e.g., missing configuration, permission issues, port already in use, dependency not met).
-
Try to restart the service:
sudo systemctl restart <service_name>
Then immediately check systemctl status <service_name> again, and if it fails, check journalctl -xeu <service_name>.
-
Check the service unit file:
The Loaded: line in systemctl status output shows the path to the service file (e.g., /lib/systemd/system/sshd.service or /etc/systemd/system/myservice.service). Examine this file for any misconfigurations.
By following these steps, you can effectively diagnose whether systemctl itself is functioning and, more commonly, troubleshoot specific services managed by systemd.