| What Are System Services? |
|
services are managed by Systemd run continuously in the background examples
The PID 1 Controller
Init System is PID 1when kernel finishes loading, it starts PID 1 in modern Linux PID1 is Systemd Systemd creates a dependency tree |
| Understanding Init Systems: From SysVinit to Systemd |
Systemd designed for speed and parallelism
|
| Managing Services with systemctl |
systemctl provides control panel
|
| Viewing Service Logs with journalctl |
|
systemd's journalctl is a centralized logging system collects output from every running service stores data in binary format to read logs use journalctl commands The Firehose (journalctl)
shows every log message from every service
journalctl Filtering by Service (-u)
to view logs from a specific service
journalctl -u <service name> Following Logs in Real Time (-f)
works like tailshows live feed of logs journalctl -u <service name> -f Filtering by Boot (-b)
see what happened during the current boot session
journalctl -bsee what happened during the previous boot session journalctl -b -1 Filtering by Priority (-p)
log priority levels are
to only see errors journalctl -p err Filtering by Time (--since)
to see logs from the last hour
journalctl --since "1 hour ago"to see logs from a specific time period journalctl --since "2025-10-30 14:00:00" --until "2025-10-30 15:00:00" Cleaning Logs
journal can grow largeto check space consumed by logs journalctl --disk-usageto clear old logs and keep only the last 1GB sudo journalctl --vacuum-size=1G |
| Understanding Units: Service, Mount, Timer, and More |
|
systemd also manages Units a service is one type of unit to see all units systemctl list-units Service Units (.service)
standard programs
Socket Units (.socket)
represents network socket of a file streamSystemd can listen on a port (like port 80 for web) without actually running the web server when a connection comes in
Mount Units (.mount)
Systemd manages the mounting of hard drives and filesystems/etc/fstab contains data entries which Systemd translates into Mount Units Timer Units (.timer)
modern replacement for cron jobsa timer unit is a schedule are more powerful than Cron because they can handle dependencies Target Units (.target)
a Target is a group of unitsused to synchronize the boot process |
| Creating Custom Service Files |
|
how to turn your own scripts into Systemd services consider a Python script called backup_monitor.py which needs to run 24/7 run it a terminal, it will stop when the terminal closes using nohup or backgrounding &, it won't restart if the computer reboots or if the script crashes need to make it a first-class citizen managed by Systemd Step 1 : Create the Script
make a dummy script for testing
nano /home/yourname/myscript.shthe script content #!/bin/bash
while true do
echo "My service is running..."
sleep 10 done
done
make the script executable
chmod +x /home/yourname/myscript.sh Step 2 : Creathe the Unit File
system configuration files live in
/etc/systemd/systementer sudo nano /etc/systemd/system/myservice.serviceadd the following content [Unit] Description=My Custom Test Service After=network.target [Service] Type=simple User=yourname ExecStart=/home/yourname/myscript.sh Restart=on-failure [Install] WantedBy=milti-user.targetexplanation of content
Step 3 : Reload and Start
need to have Systemd to scan for script
sudo systemctl daemon-reloadstart the service sudo systemctl start myservicecheck the status sudo systemctl status myserviceshould se Active (running)check the logs journalctl -u myservice Step 4 : Test Resilence
verify the restart policykill the process manually use PID from status output kill -9 <PID>wait a moment to give Systemd to restart the service check status again service should be running with a new PID Step 5 : Clean Up
stop and disable service
sudo systemctl stop myservice sudo systemctl disable myservice sudo rm /etc/systemd/system/myservice.service sudo systemctl daemon-reload |
| System Startup and Boot Process Overview |
|
sequence of events when a Linux box gets booted
Stage 1: BIOS /UEFI
when powered on the motherboard firmware (BIOS or UEFI) wakes upperforms a POST (Power On Self Test) to ensure RAM and CPU are working looks for a bootable device (Hard Drive, USB, Network) Stage 2: The Bootloader (GRUB)
firmware hands control to the Bootloaderon most Linux systems, this is GRUB (Grand Unified Bootloader) GRUB's job is simple: Load the Linux Kernel into memory Stage 3: The Kenel
initializes the hardware (drivers for graphics, disks, network)the Kernel mounts the root filesystem (/) the Kernel executes the very first program: /sbin/init (which is a link to Systemd) Stage 4: Systemd (PID 1)
Systemd takes overreads its "Target" configuration the default target is graphical.target Systemd calculates dependencies works backward from the goal and starts everything required to reach it Boot Target (Runlevels)
used to use Runlevels to define target typesSystemd uses targets mapping to similar concepts
Changing Targets
to change targets immediately
sudo systemctl isolate multi-user.targetto change default target for next boot sudo systemctl set-default multi-user.target |
| Troubleshooting Service Failures |
|
an administrator will spend a lot of time fixing services which refuse to start a systematic workflow for trouble shooting Scenario
Nginx web server is installed but not loading
|
| Summary |
covered
key points
|