Where to start First steps track · 15 stages
Linux Labs

Service

5 min Linux

What a service is, how it differs from an ordinary program, and how to manage it.

Cheat sheet

8 commands Printable version
  • systemctl status nginx --no-pager the service's state
  • sudo systemctl status nginx + recent journal lines
  • sudo systemctl start nginx start it now
  • sudo systemctl stop nginx stop it now
  • sudo systemctl restart nginx restart it
  • sudo systemctl enable nginx start automatically at boot
  • sudo systemctl enable --now nginx enable at boot and start it right away
  • sudo journalctl -u nginx -e this service's journal

Overview

A service is a program that runs in the background without direct user interaction and performs some function for the system or the network: a web server answers requests, a database stores data, sshd accepts connections. Unlike a command you run and wait for, a service keeps running (until the next reboot or a deliberate stop) and normally survives you closing the terminal you started it from — because it isn't the terminal that starts and watches it, it's the init system.

On modern Linux distributions that role belongs to systemd — the system's first process (PID 1). A service is described by a unit file (.service) and managed with systemctl:

bash
systemctl status nginx
# ● nginx.service - A high performance web server
#      Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
#      Active: active (running) since ...

This article covers the service as a concept: what it is and how to manage it at a basic level. Unit files, dependencies, targets, and socket activation are covered in systemd. The full set of systemctl commands (mask, isolate, edit) is in systemctl. Service logs are in journalctl.

Recipes

3

Check whether a service is running

bash
systemctl status nginx --no-pager  # state in words, no pager
sudo systemctl status nginx --no-pager   # + recent journal lines
systemctl is-active nginx          # just active/inactive/failed (for scripts)

Start, stop, and enable at boot

bash
sudo systemctl start nginx         # now, until the next reboot
sudo systemctl stop nginx          # stop it now
sudo systemctl enable nginx        # start at the next boot (doesn't start it now)
sudo systemctl enable --now nginx  # both in one command
sudo systemctl disable nginx       # remove it from autostart

See what the service has logged

bash
sudo journalctl -u nginx -e        # the service's journal, at the end
sudo journalctl -u nginx --since "10 min ago"

Troubleshooting

5

The service "crashed", but systemctl status shows active

You may be looking at a different service or a renamed/aliased unit (a .socket instead of a .service). Check the exact name: systemctl list-units '*nginx*'.

enable didn't start the service right away

enable only sets up autostart for the future (it creates a symlink). To start it immediately too, run start separately, or use enable --now in one command.

The service keeps restarting on its own (a restart loop)

It's crashing after starting, and systemd is configured to restart it (Restart= in the unit file). The reason is in the journal: journalctl -u <service> -e.

Warning: some journal files were not opened due to insufficient permissions.

Journal lines in systemctl status are visible only with sudo or to members of the adm and systemd-journal groups. Without them, status prints the state and this warning. Repeat with sudo — see systemctl and journalctl.

What's the difference between "service", "daemon", and "unit"?

A daemon is a background process in the general sense (the term predates systemd). A unit is an object systemd manages; a service (.service) is the most common kind of unit, but there are others (.socket, .timer, .mount). A service almost always runs a daemon.

See also

  • systemd — The init system and service manager — units, targets, sockets, timers.
  • systemctl — Managing systemd services — start/stop/restart/status/enable/reload.
  • journalctl — Reading the systemd journal (journald) — filters by service, time, priority.
Loading…