๐Ÿ“˜ Visual Posts About Contact
Explore ๐Ÿ“‹ Cheat Sheets โœ๏ธ Practice ๐ŸŽฎ Games ๐ŸŽฌ Simulations ๐Ÿƒ Flashcards ๐ŸŽฏ Interactive Layers ๐Ÿ“– Storytelling
๐Ÿ’œ Support my site

Services, Daemons & systemd

Module 07: Aโ€“Z Server Course 2026 Updated

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 โ†“
PID 1: SERVICE MANAGER Linux: systemd | Windows: services.exe (SCM) sshd Port 22 โ— active nginx Port 443 โ— active postgres Port 5432 โ— active myapp Custom Unit โ†ป auto-heal CENTRALIZED LOGGING journalctl -u nginx -f | Windows Event Viewer

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!"

sshd

SSH Daemon

Listens on Port 22 in the background so you can remotely log in with the ssh client.

httpd

HTTP Daemon

Apache's background web server process serving web pages over HTTP/HTTPS 24/7.

crond

Cron Daemon

Wakes up every minute in the background to check if scheduled backup jobs need to run.

FOREGROUND APP (Day Guest) vim, nano, Chrome, Notepad Tied to Your Terminal / Desktop Needs an active user session Dies on Logout Close the SSH window โ†’ process stops Started Manually by Human Does not auto-restart after a reboot BACKGROUND DAEMON (Night Staff) sshd, nginx, systemd, Spooler Detached from Terminal No GUI window, runs in background 24/7 Survives Logouts & Reboots Adopted & watched by PID 1 (systemd) Naming Clue: Ends in "d" sshd, httpd, dockerd, journald

๐Ÿง  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.
1. RUNTIME CONTROL (Happens Right Now) start Launch process Assigns new PID stop Graceful exit Sends SIGTERM restart Stop + Start Brief downtime reload Refresh config Zero downtime! 2. BOOT PERSISTENCE (Happens After Reboot) enable Creates boot symlink Auto-starts on power on disable Removes boot symlink Stays off on reboot enable --now Pro Shortcut! Enables + Starts together

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 start and systemctl enable?
  • Tell why reload is safer than restart on a busy production web server?
  • Identify what PID 1 is 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?).

/etc/systemd/system/myapp.service [Unit] Description=My Python API After=network.target Identity & Ordering Waits until networking is ready before starting. [Service] User=www-data ExecStart=/usr/bin/python3 app.py Restart=on-failure Execution & Self-Healing Runs as non-root user (least privilege) and automatically restarts if the app crashes. [Install] WantedBy=multi-user.target Boot Target Link Tells systemctl enable when to launch at boot.

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

AUTO

Automatic

Starts immediately when Windows boots (e.g., DNS Client, Print Spooler). Use Automatic (Delayed Start) for heavy apps so boot stays fast.

MAN

Manual

Stays stopped at boot until a user or another dependent service explicitly triggers it.

DIS

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).

1. SERVICES nginx stdout / stderr sshd auth attempts Spooler Windows API 2. LOG COLLECTORS systemd-journald Indexed binary logs + /var/log/syslog + /var/log/nginx/ Windows Event Log System & Application eventlog service 3. ADMIN TOOLS journalctl -u nginx -f (Live) -p err (Errors only) --since "1 hour ago" eventvwr.msc Windows Logs โ†’ System PS: Get-WinEvent

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 .service unit file?
  • Manage a Windows Service using both services.msc and PowerShell's Get-Service?

06 Knowledge Check

Test your Services, Daemons & systemd knowledge โ€” click an option for instant feedback. Score: 0 / 4

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โœ…
Rebooting automatically deletes /etc/nginxโŒ
Nginx can only run while an admin stays logged in via SSHโŒ

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โŒ