How to manage services with systemd
Topic: Servers linux
Summary
Start, stop, restart, and enable systemd units; check status and read logs with journalctl. Use this to bring services up after boot, restart after config changes, and diagnose why a unit failed.
Intent: How-to
Quick answer
- systemctl start/stop/restart/reload UNIT; systemctl enable/disable UNIT for boot; systemctl status UNIT and journalctl -u UNIT -f for logs.
- After editing a unit or drop-in, run systemctl daemon-reload then restart the unit; config errors often show in status.
- Failed state: read journalctl -u UNIT --no-pager; fix config or deps (sockets, paths, mounts) then restart.
Prerequisites
Steps
-
List and inspect units
systemctl list-units --type=service; systemctl status nginx; systemctl show nginx for full properties; list unit files in /etc/systemd/system and /lib/systemd/system.
-
Start, stop, enable
systemctl start nginx; systemctl stop nginx; systemctl enable nginx (enable at boot); systemctl disable nginx; use reload when the unit supports it (e.g. nginx -s reload).
-
Edit and reload
Edit unit in /etc/systemd/system (prefer drop-in .d); run systemctl daemon-reload; then systemctl restart UNIT so changes apply.
-
Read logs
journalctl -u nginx -f for follow; journalctl -u nginx -b for this boot; journalctl -u nginx -n 100 --no-pager for last 100 lines.
Summary
You will manage systemd services: start, stop, enable at boot, reload config, and read logs with journalctl. Use this whenever you install a daemon or need to change or debug a service.
Prerequisites
- Root or sudo; systemd as init (default on modern Linux).
Steps
Step 1: List and inspect units
systemctl list-units --type=service --state=running
systemctl status nginx
systemctl cat nginx
Use systemctl list-unit-files to see all units; check /etc/systemd/system for overrides.
Step 2: Start, stop, enable
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx # if supported
sudo systemctl enable nginx
sudo systemctl disable nginx
Enable means “start at boot”; disable only removes that, it does not stop the service now.
Step 3: Edit and reload
sudo systemctl edit nginx # creates override in /etc/systemd/system/nginx.service.d/
# or
sudo cp /lib/systemd/system/nginx.service /etc/systemd/system/
sudo nano /etc/systemd/system/nginx.service
sudo systemctl daemon-reload
sudo systemctl restart nginx
Always daemon-reload after changing unit files.
Step 4: Read logs
journalctl -u nginx -f
journalctl -u nginx -b -n 200 --no-pager
journalctl -xe
Use -p err to filter by priority; -S / -U for time range.
Verification
- Service is active (running) and enabled where desired; after edit, restart succeeds and logs show no errors.
Troubleshooting
Unit not found — Name is usually the service name; use systemctl list-unit-files | grep name; ensure the unit file is under /etc or /lib and run daemon-reload.
Failed to start — journalctl -u unit shows the error; common: missing binary, bad config, failed dependency (socket, mount); fix and restart.
Reload had no effect — Some units do not support reload; use restart; or you edited the wrong file or forgot daemon-reload.