Service Management¶
Prerequisites¶
- SSH access to the target machine
- Root or sudo privileges
Quick Reference¶
| Action | Command |
|---|---|
| List running | systemctl list-units --type=service |
| List all | systemctl list-unit-files --type=service |
| Status | systemctl status <svc> |
| Logs | journalctl -u <svc> |
| Follow logs | journalctl -u <svc> -f |
| Start | systemctl start <svc> |
| Stop | systemctl stop <svc> |
| Restart | systemctl restart <svc> |
| Enable (boot) | systemctl enable <svc> |
| Reload config | systemctl daemon-reload |
Procedure: List and Filter Services¶
When to use: Finding out what services exist on the system, which are running, and which are enabled.
Steps:
-
List all running services:
systemctl list-units --type=service -
List all services (including inactive/disabled):
systemctl list-unit-files --type=service -
Filter by name:
systemctl list-unit-files --type=service | grep <keyword> -
Check if a specific service is active:
systemctl is-active <service_name> -
Check if a specific service is enabled on boot:
systemctl is-enabled <service_name>
Troubleshooting:
- If a service you installed doesn't appear: Run
systemctl daemon-reloadto rescan unit files.
Procedure: Check Service Status and Logs¶
When to use: A service failed to start, crashed, or is behaving unexpectedly.
Steps:
-
Check current status and recent log lines:
Look forsystemctl status <service_name>Active: active (running)orfailed. -
View full logs for a specific service:
journalctl -u <service_name> -
View live logs (follow mode):
journalctl -u <service_name> -f -
View logs from the current boot session only:
journalctl -u <service_name> -b
Troubleshooting:
- If logs are truncated: Use
journalctl -u <service_name> --no-pageror piping toless. - If no logs appear: Ensure the service is actually logging to stdout/stderr or syslog.
Procedure: Start/Stop/Restart a Service¶
When to use: You need to start a daemon, stop a misbehaving service, or apply configuration changes (requires restart).
Steps:
-
Start a service immediately:
systemctl start <service_name> -
Stop a running service:
systemctl stop <service_name> -
Restart a service (stop then start):
systemctl restart <service_name> -
Reload configuration without interrupting connections (if supported):
systemctl reload <service_name> -
Verify the status:
systemctl status <service_name>
Troubleshooting:
- If
systemctlhangs: Check if the service is stuck in a loop or waiting for resources. - If "Unit not found": Check spelling with
systemctl list-unit-files | grep <name>.
Procedure: Enable a Service on Boot¶
When to use: Ensure a service starts automatically when the server reboots (e.g., web server, database).
Steps:
-
Enable the service:
Note: This usually creates a symlink insystemctl enable <service_name>/etc/systemd/system/multi-user.target.wants/. -
Disable the service (prevent autostart):
systemctl disable <service_name> -
Enable and Start in one command:
systemctl enable --now <service_name> -
Verify enabled status:
systemctl is-enabled <service_name>
Troubleshooting:
- If a service is enabled but fails to start on boot: Check
journalctl -u <service_name> -bto see logs from the current boot.
Procedure: Create a Custom systemd Unit File¶
When to use: You want to run a custom script or application as a managed service.
Steps:
-
Create a unit file at
/etc/systemd/system/myapp.service:[Unit] Description=My Custom Application After=network.target [Service] Type=simple User=myuser ExecStart=/usr/bin/python3 /opt/myapp/main.py Restart=on-failure [Install] WantedBy=multi-user.target -
Reload systemd to recognize the new file:
systemctl daemon-reload -
Start and enable the service:
systemctl enable --now myapp
Troubleshooting:
- If modifications aren't picked up: Did you run
systemctl daemon-reload? - If
Exec format error: EnsureExecStartuses absolute paths (e.g.,/usr/bin/python3, not justpython3).
Related Documentation¶
- Technologies: systemd
- Concepts: Configuration Management