Services and logs

Control a systemd service

Inspect, start, stop, restart, reload, enable, and disable a service while choosing the least disruptive action.

8 minute lesson

~~~

Systemd manages long-running services on Ubuntu. A unit describes how a service starts, stops, restarts, and relates to other parts of the system.

Start by inspecting the current state:

systemctl status nginx --no-pager
systemctl is-active nginx
systemctl is-enabled nginx

active describes the current runtime state. enabled describes whether the unit is configured to start through its boot dependencies. A service can be active but disabled, or enabled but currently failed.

Choose the least disruptive action

Use start and stop to change the current runtime state:

sudo systemctl start nginx
sudo systemctl stop nginx

restart stops and starts the service. Active connections may be interrupted.

reload asks a running service to reread configuration without a full restart:

sudo systemctl reload nginx

Not every service supports reload. Check its documentation and unit definition. A successful reload command also does not prove the new configuration is valid or serving the intended site.

Boot behavior is separate

sudo systemctl enable nginx
sudo systemctl disable nginx

These commands change future boot behavior. Add --now only when you intentionally want to change both boot and current runtime state.

Validate before restart or reload

Use the application’s own check first:

sudo nginx -t
sudo systemctl reload nginx

Then verify the unit, logs, listening port, and real request:

systemctl status nginx --no-pager
sudo journalctl -u nginx -n 30 --no-pager
ss -lnt
curl -I http://127.0.0.1/

If the new behavior fails, restore the last working configuration and reload again. Do not keep restarting a service without reading the error.

Try this: choose one harmless service on a test machine. Record whether it is active and enabled, find its unit definition with systemctl cat, and identify its validation command.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →