systemd and Daemons
Level 3 · Software and services
What a daemon is and how systemd starts, stops and restarts it. How to read a service's log with journalctl when something goes wrong.
Course contentsLesson 8 of 10
Level 1 · Typing commands
Level 2 · Files, users and permissions
Level 3 · Software and services
Level 4 · Timed jobs and the firewall
After this lesson you can
- Start, stop and restart a service
- Make a service start at boot
- Read a service's log with journalctl
12 mincovers 3 glossary entries
Worth reading first: APT and .deb Packages
How Linux keeps programs running in the background
A daemon is a program that runs in the background all the time, with no window. The web program, the login service and the job timer are all daemons. On almost every Linux system, a program called systemd starts them, watches them and restarts them if they crash. You talk to it with the systemctl command.
systemd is the first program the system starts, so it has the number 1. Every other program hangs below it. Each daemon is described to it as a service, with a name such as nginx or ssh.
Four commands do most of the work. sudo systemctl start nginx starts it. stop stops it. restart stops it and starts it again, which you do after changing its settings. systemctl status nginx shows whether it is running, since when, and its last few log lines.
Starting a service now is not the same as starting it at boot. sudo systemctl enable nginx makes it start each time the machine starts. Forget this, and the service runs fine until the first restart. Then it stays off, and nothing tells you.
Every daemon writes notes about what it is doing. systemd collects them all in one place, called the journal. journalctl reads it. journalctl -u nginx shows only one service. Adding -f keeps the window open and prints new lines as they arrive.
When a service will not start, the reason is nearly always in the journal. It may be a typo in a settings file or a missing file. Or another program may already be using its address. Read the last 20 lines before you change a thing.
A common mistake is to restart a service again and again, hoping it will work. If it failed once, it will fail the same way. Read the journal, fix the cause, and then restart it once.
$ sudo systemctl restart nginx
$ systemctl is-active nginx
active
$ journalctl -u nginx -n 20Worked example
Jack's site is down after a change
Jack edits his web program's settings and restarts it. The site stops loading, and systemctl status says failed.
journalctl -u nginx -n 20 shows the cause on the last line: a missing semicolon on line 14. He adds it, restarts once, and the site is back.
The words for this lesson3
Check yourself
A service runs now, but stops after every reboot. What was missed?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.