Service
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-pagerthe service's state -
sudo systemctl status nginx+ recent journal lines -
sudo systemctl start nginxstart it now -
sudo systemctl stop nginxstop it now -
sudo systemctl restart nginxrestart it -
sudo systemctl enable nginxstart automatically at boot -
sudo systemctl enable --now nginxenable at boot and start it right away -
sudo journalctl -u nginx -ethis 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:
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
3Start, stop, and enable at boot
See what the service has logged
Troubleshooting
5The 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.
Found an inaccuracy? Report an error in the article