Services, Daemons & systemd
When you close your laptop or log out of a server, websites, databases, and SSH logins must keep running 24/7. Meet Daemons, systemd, and Windows Servicesโthe invisible night-shift staff that never sleep.
Start Module 07 โ01 What Is a Daemon / Service?
Define: A daemon (in Linux/Unix) or a service (in Windows) is a computer program that runs silently in the background without any user interface, terminal window, or mouse clicks required.
Understand: When you open a text editor or web browser, it runs in the foregroundโit belongs to your login session. The moment you close the window or log out, that program dies. A server cannot work like that! Your web server, database, and SSH login listener must start automatically when the computer powers on and keep running even when zero humans are logged in.
๐จ Real-World Analogy: The 24/7 Night-Shift Staff
Imagine a busy international hotel. Daytime guests (foreground apps like
Chrome or nano) check in, use a room, and leave. Meanwhile, the 24/7
Night-Shift Staff (daemons and services) work behind the scenesโsecurity guards at
the front door (sshd), HVAC engineers keeping the building cool, and the front-desk
system answering calls at 3:00 AM (nginx). You don't see them sitting in your
lobby chair, yet the hotel collapses the second they clock out.
Know: Why Do Linux Daemons End in d?
In Linux, you will constantly see program names ending with the letter d.
That trailing d simply stands for Daemonโa helpful
naming convention telling you, "Hey, this is the background worker, not the interactive
client!"
SSH Daemon
Listens on Port 22 in the background so you can remotely log in with the ssh
client.
HTTP Daemon
Apache's background web server process serving web pages over HTTP/HTTPS 24/7.
Cron Daemon
Wakes up every minute in the background to check if scheduled backup jobs need to run.
๐ง Memory Trick: "D is for Desk-Free"
Whenever you see a trailing d (like
sshd or httpd), remember: D = Daemon = Desk-Free. It
doesn't sit at a desk with a monitor and keyboard; it works silently in the server room 24/7.
02 Linux systemd & systemctl
Define: systemd is the master system and service
manager on almost every modern Linux distribution (Ubuntu, Debian, RHEL, Rocky, AlmaLinux, Fedora).
It always gets PID 1 (Process ID 1)โthe very first process the Linux kernel starts
when booting, and the parent of all other services.
Understand: Think of systemd as the Hotel General
Manager, and systemctl as the walkie-talkie
you use to give orders to that manager. You never manually hunt down a daemon's PID just to restart
itโyou tell systemctl what you want, and systemd handles the rest cleanly.
Know: The 7 Essential systemctl Commands
Every beginner confuses start vs enable.
Here is the golden rule: start/stop control what happens right
now, while enable/disable control what happens after
a reboot.
| Command | Scope | What It Actually Does |
|---|---|---|
sudo systemctl start nginx |
Right Now | Starts the service immediately (does not survive a reboot unless enabled). |
sudo systemctl stop nginx |
Right Now | Cleanly shuts down the running service immediately. |
sudo systemctl restart nginx |
Right Now | Stops and starts the service (brief connection drop; new PID). |
sudo systemctl reload nginx |
Right Now | Re-reads config files without dropping active connections. |
sudo systemctl enable nginx |
At Boot | Auto-starts the service every time the server boots up. |
sudo systemctl disable nginx |
At Boot | Prevents the service from starting automatically at boot. |
systemctl status nginx |
Inspect | Shows if it's active, its PID, memory usage, and the last ~10 log lines. |
When & Execute: Choosing reload vs restart
When you edit a web server config (like adding a new website to Nginx), use
sudo systemctl reload nginx first. It tells Nginx to re-read its config file without
kicking off visitors currently downloading files. Only use restart when you change
environment variables, upgrade the binary, or the service is frozen.
# Pro move: Enable at boot AND start immediately in one command
sudo systemctl enable --now nginx
# Check health without pager clutter
systemctl status nginx --no-pager
โ Checkpoint: Can you...
- Explain the difference between
systemctl startandsystemctl enable? - Tell why
reloadis safer thanrestarton a busy production web server? - Identify what
PID 1is on a modern Linux server?
03 Anatomy of a systemd Unit File
Define: A systemd Unit File is a plain-text configuration file
(ending in .service) that teaches systemd how to start, stop, and
auto-heal a background program.
Understand: Think of a Unit File as a Job Description Card for a
new night-shift employee. It answers three simple questions in three sections:
[Unit] (Who are you and who must arrive before you?),
[Service] (What exact command do you run, and what if you crash?),
and [Install] (Which shift/boot level do you belong to?).
Know & Execute: Creating Your Own Background Service
Custom services always go in /etc/systemd/system/ (never edit vendor
files in /lib/systemd/system/ directly, because OS updates will overwrite them!).
Whenever you create or edit a unit file, run systemctl daemon-reload
so systemd reads your new file.
# 1. Create /etc/systemd/system/myapp.service
[Unit]
Description=My Custom Background API
After=network.target
[Service]
Type=simple
User=appuser
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/python3 /opt/myapp/server.py
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
# 2. Reload systemd, then enable and start your new service
sudo systemctl daemon-reload
sudo systemctl enable --now myapp.service
๐ง Memory Trick: "U-S-I = Who, How, When"
[U]nit = Who am I & what do I wait for? | [S]ervice = How do I run & recover? | [I]nstall = When do I start at boot?
04 Windows Services
Define: On Windows Server and Windows 11, background daemons are called
Windows Services, managed by the Service Control Manager (SCM)
running as services.exe.
Understand: While Linux uses systemctl and text unit files, Windows
stores service definitions in the Windows Registry
(HKLM\SYSTEM\CurrentControlSet\Services) and lets you manage them via a graphical
console (services.msc), PowerShell (Get-Service), or the classic command
line (sc.exe).
Know: Startup Types & Recovery Actions
Automatic
Starts immediately when Windows boots (e.g., DNS Client, Print Spooler). Use Automatic (Delayed Start) for heavy apps so boot stays fast.
Manual
Stays stopped at boot until a user or another dependent service explicitly triggers it.
Disabled
Cannot be started by anythingโnot even an adminโuntil you change its Startup Type back.
๐ก๏ธ Pro Tip: Configure the Recovery Tab!
Just like Restart=on-failure in Linux, every Windows
Service has a Recovery tab inside services.msc. Always set
First failure โ Restart the Service for critical business apps so a random crash
self-heals in seconds instead of waking you up at 2:00 AM.
Execute: Managing Windows Services from PowerShell & CMD
# PowerShell: List all running services
Get-Service | Where-Object { $_.Status -eq 'Running' }
# PowerShell: Restart the Print Spooler and set it to Automatic
Restart-Service -Name Spooler
Set-Service -Name Spooler -StartupType Automatic
# Classic CMD (sc.exe): Query status or configure startup type
sc.exe query Spooler
sc.exe config Spooler start= auto
05 Reading Service Logs
Define: Service logs are the chronological flight-recorder diaries written by background processes so administrators can see why a service failed to start or crashed overnight.
Understand: Because background services don't have a screen to print error popups,
they send every warning and error to a centralized log collector. On modern Linux, that collector is
systemd-journald (queried with journalctl),
alongside classic plain-text files in /var/log/. On Windows, it is the
Windows Event Viewer (eventvwr.msc).
Execute: Real-World Log Commands You Will Use Every Week
# Follow live Nginx logs in real time (-u = unit, -f = follow)
sudo journalctl -u nginx -f
# Show only errors (-p err) from today for SSH
sudo journalctl -u sshd -p err --since today
# Classic plain-text logs on Debian/Ubuntu vs RHEL/Rocky
tail -f /var/log/syslog # Debian / Ubuntu
tail -f /var/log/messages # RHEL / Rocky / AlmaLinux
๐ Practice Problem: The Crashing Web Server
Given: You just edited /etc/nginx/nginx.conf and ran
sudo systemctl restart nginx, but it failed with "Job for nginx.service
failed because the control process exited with error code."
Find: Which two commands should you run to see the exact line number where the syntax error happened, and how should you apply config changes safely in the future?
Show Solution
Step 1 (Inspect the Error Logs): Run
sudo journalctl -u nginx -n 30 --no-pager (or
systemctl status nginx) to read the exact error message and line number.
Step 2 (Test Before Reloading): Run sudo nginx -t to
validate syntax before touching the live service.
Step 3 (Zero-Downtime Apply): Once fixed, use
sudo systemctl reload nginx instead of restart so active
visitors are not disconnected.
โ Checkpoint: Can you...
- Follow live logs for a specific Linux service using
journalctl -u <name> -f? - Name the three main sections of a
.serviceunit file? - Manage a Windows Service using both
services.mscand PowerShell'sGet-Service?
06 Knowledge Check
1. You ran sudo systemctl start nginx, and your website is online. However,
after you reboot the server, Nginx is offline again. Why?
start only runs the service right now; you forgot to run
systemctl enable nginxโ
/etc/nginxโ
2. In a custom .service unit file, which section defines the command to run
(ExecStart) and crash recovery (Restart=on-failure)?
[Unit]โ
[Service]โ
[Install]โ
3. Which PowerShell cmdlet lists Windows Services and their current status (Running or Stopped)?
journalctl -uโ
Get-Serviceโ
Get-ChildItemโ
4. You want to watch live log entries for the nginx service in real time on
Linux. Which command do you run?
sudo systemctl daemon-reload nginxโ
sudo journalctl -u nginx -fโ
sc.exe query nginxโ