How to Manage Services With systemd

Engineer checking servers in a data center aisle

To manage services with systemd, you use systemctl to start, stop, enable and inspect units, and journalctl to read their logs. This Mintopsblog.com guide covers the everyday commands, unit files and the troubleshooting steps that solve most service problems.

Key Takeaways

  • systemd starts and supervises services on most current Linux distributions.
  • systemctl start/stop act now; enable/disable control what happens at boot.
  • Unit files describe a service; put local changes in drop-in overrides, not the vendor file.
  • journalctl -u name shows the logs of one service and is the first stop when something fails.

What systemd Does

systemd is the init system and service manager on Ubuntu, Debian, Fedora, RHEL and most other mainstream distributions. It is the first process the kernel starts, and it brings up everything else: mounts, networking, logging and your applications. Each thing it manages is a “unit”, and a running program such as a web server or database is a service unit with a .service suffix.

Because systemd supervises services, it can restart them when they crash, start them in the right order, limit their resources and capture everything they print to standard output in the journal.

Everyday systemctl Commands

The systemctl manual lists dozens of verbs, but a short set covers daily work.

CommandWhat it does
systemctl status nginxShows whether the service runs, its main PID and the latest log lines
sudo systemctl start nginxStarts the service now
sudo systemctl stop nginxStops the service now
sudo systemctl restart nginxStops and starts it, for example after a package update
sudo systemctl reload nginxAsks the service to re-read its configuration without a full restart, if it supports reload
sudo systemctl enable --now nginxStarts it at every boot and also starts it immediately
sudo systemctl disable nginxStops it from starting at boot (does not stop it now)
systemctl list-units --type=service --state=failedLists services that have failed

Remember the split: start and stop change the current state, while enable and disable change the boot behavior. A service can be running but disabled, which means it will be gone after the next reboot. That surprise causes many “it worked yesterday” tickets.

Reading Unit Files

A unit file is a plain text file in INI style. Vendor files live in /usr/lib/systemd/system/ (or /lib/systemd/system/ on Debian-based systems), and local files go in /etc/systemd/system/, which takes priority. The systemd.service documentation explains each option; these are the ones you will meet most often:

  • [Unit] Description= and After= describe the service and its ordering, for example after the network is online.
  • [Service] ExecStart= is the command that runs the program; User= and Group= run it as an unprivileged account.
  • Restart=on-failure tells systemd to bring the service back if it exits with an error.
  • Environment= or EnvironmentFile= pass settings without editing the program.
  • [Install] WantedBy=multi-user.target decides which target pulls the service in when you enable it.

Overriding a Service Safely

Never edit the vendor unit directly, because the next package update will overwrite it. Run sudo systemctl edit nginx instead. It opens an override file in /etc/systemd/system/nginx.service.d/ where you add only the lines you want to change, such as a higher file limit or a different restart policy. After any manual change to unit files, run sudo systemctl daemon-reload so systemd reads them again.

Checking Logs With journalctl

systemd collects service output in the journal. The journalctl reference covers filters; these four handle most cases:

  • journalctl -u nginx shows all logs for one unit.
  • journalctl -u nginx -f follows new lines live, like tail -f.
  • journalctl -u nginx --since "1 hour ago" limits the time range.
  • journalctl -p err -b shows only errors since the current boot.

On a single server, the journal is enough. Once you run several machines, ship those logs to one place; the centralized logging guide explains how.

A Troubleshooting Routine That Works

  1. Run systemctl status and read the last lines: the exit code and message often name the problem.
  2. Read the full logs with journalctl -u name -e to see the lines just before the failure.
  3. Test the configuration with the program’s own checker, such as nginx -t, before restarting again.
  4. Check file ownership and modes for anything the service reads or writes; the Linux file permissions guide explains the usual causes.
  5. Confirm the port is free with ss -tlnp if the log mentions “address already in use”.
  6. After fixing, run daemon-reload if you changed a unit, then restart and watch the status.

You can find more articles like this one in the Linux category, and tooling topics in DevOps and Data Tools. All Mintopsblog.com tutorials are listed on the Mintopsblog.com home page.

Timers: A Modern Alternative to Cron

systemd can also run jobs on a schedule. A timer unit, such as backup.timer, activates a matching backup.service at the times you set with OnCalendar=, for example daily or Mon *-*-* 02:00. Timers log to the journal like any service, can catch up on missed runs with Persistent=true, and show their next run time in systemctl list-timers. Cron still works everywhere and is fine for simple tasks, but timers make it easier to see whether a scheduled job failed and why, which is a common question on busy servers.

Resource limits are another reason to define jobs as units. Settings such as MemoryMax= and CPUQuota= stop a runaway script from starving the rest of the system.

Frequently Asked Questions

What Is the Difference Between Start and Enable in systemd?

start runs the service immediately but says nothing about the next boot. enable creates the links that make systemd start the service automatically at boot, but does not start it now. Use systemctl enable –now when you want both, which is the usual choice for a new service.

Why Do I Need to Run daemon-reload?

systemd keeps unit files in memory. When you create or edit a unit or override file by hand, it will not notice until you run systemctl daemon-reload. Without it, a restart uses the old definition. systemctl edit runs the reload for you when you save and close the editor.

How Do I See Why a Service Failed?

Start with systemctl status name, which shows the exit code and the last log lines. Then run journalctl -u name -e to see the full log around the failure. Most failures come from a configuration syntax error, a missing file, a permission problem or a port already in use.

Where Should I Put My Own Unit Files?

Place custom units and overrides in /etc/systemd/system/. Files there take priority over vendor units in /usr/lib/systemd/system/ and survive package updates. For small changes to an existing service, use systemctl edit to create a drop-in override instead of copying the whole unit file.

Search
Start With the Mintopsblog.com Guide

See every topic on one page: Linux, DevOps and data tools, security and SSL, and how to use older tutorials safely.

Related Articles