The Heartbeat of Linux
At the very core of every Linux system beats a special process - PID 1, the init system. This is the first process started by the kernel and the last one to die when the system shuts down. It's the ancestor of all other processes, the supervisor of system services, and the orchestrator of your system's lifecycle.
Think of the init system as the conductor of an orchestra. While the kernel provides the instruments (hardware resources), the init system coordinates when each musician (service) plays, ensuring they work in harmony. Some conductors (SysV Init) follow a strict, sequential score, while others (systemd) allow sections to play simultaneously for a faster, more dynamic performance.
The evolution from SysV Init to systemd represents one of the most significant changes in Linux history, transforming how we think about system initialization and service management.
The figure below compares the major init systems. Switch views to compare systemd, SysV Init, OpenRC and Upstart, step through a boot, start services one by one, or see how systemd and SysV Init are built.
- Fast boot times
- Feature-rich
- Active development
- Good documentation
- Complex
- Large codebase
- Controversial design
- Binary logs
| Feature | systemd | SysV Init | OpenRC | Upstart |
|---|---|---|---|---|
| Boot Speed | Fast | Slow | Medium | Medium |
| Parallel Start | Yes | No | Yes | Yes |
| Dependencies | Automatic | Manual | Automatic | Event-based |
| Complexity | High | Low | Medium | Medium |
The Init Process Hierarchy
PID 1: The Immortal Process
The kernel searches for init in order: /sbin/init, /etc/init, /bin/init, /bin/sh. If none found, kernel panics. PID 1 never exits - it's the ancestor of all processes and adopts orphans.
# View process tree
pstree -p
# systemd(1)───systemd-journal(289)
# ├──systemd-udevd(315)
# ├──sshd(890)───bash(1235)
# └──nginx(1001)───nginx(1002)
SysV Init: The Traditional Approach
Sequential startup: Services start one at a time based on numeric order. Simple but slow.
Runlevels: System states controlling which services run.
- 0: Halt/Shutdown
- 1: Single user mode (rescue)
- 3: Multiuser with network
- 5: Multiuser with GUI
- 6: Reboot
# Change runlevel
init 3
telinit 5
# Check current
runlevel
Service Management
Scripts in /etc/init.d/ handle start, stop, restart. Simple shell scripts with case statements.
# Start/stop services
service httpd start
service httpd stop
# Enable/disable at boot
chkconfig httpd on
chkconfig --list
systemd: The Modern Powerhouse
Parallel startup: Services start simultaneously when dependencies met. Fast and efficient.
Unit Files: Declarative Configuration
Unlike SysV's shell scripts, systemd uses declarative unit files. Each directive has a specific meaning. The figure shows a service, a timer and a socket unit; select a directive to see what it does and how much it matters.
- [Unit]
- Generic info and dependencies. Applies to all unit types.
- [Service]
- Type-specific configuration. Defines how the unit operates.
- [Install]
- Defines how "systemctl enable" installs the unit.
Socket Activation: On-Demand Services
One of systemd's most powerful features is socket activation. Instead of starting services at boot, systemd creates sockets and only spawns services when connections arrive. This dramatically speeds up boot time and saves memory. Connections queue in the kernel while the service starts, so none are lost, and the socket exists before the service, which simplifies dependencies. Step through the phases, from the socket created at boot to the service handling requests.
- 1System Boot
- 2Idle
- 3First Request
- 4Service Starting
- 5Socket Handoff
- 6Running
Step forward to see socket activation in action
System Boot: systemd creates socket, service not started
Process Supervision: Automatic Recovery
systemd monitors services and automatically restarts them based on configurable policies. Understanding these policies is crucial for building reliable systems. Pick a policy and a restart delay, then simulate an exit to see whether systemd restarts the service.
Restart=on-failure: Restart on non-zero exit, signal, timeout. RestartSec is the delay between service exit and restart attempt.
| Policy | Clean Exit | Error Exit | Crash (SIGSEGV) | Abort (SIGABRT) | Killed (SIGKILL) |
|---|---|---|---|---|---|
| no | |||||
| on-success | |||||
| on-failure | |||||
| on-abnormal | |||||
| on-abort | |||||
| on-watchdog | |||||
| always |
Best practice: use Restart=on-failure for most services. It restarts on crashes but respects intentional stops. Combine with RestartSec=5s to prevent rapid restart loops and StartLimitBurst=3 to give up after repeated failures.
Unit Types
.service: System services (nginx, postgresql).socket: Network sockets for activation.target: Groups of units (like runlevels).timer: Scheduled tasks (replaces cron).mount: Filesystem mount points.path: Path-based activation
systemd Commands
# Service management
systemctl start nginx
systemctl status nginx
systemctl enable nginx
# System state
systemctl list-units --failed
systemctl get-default
# Logs
journalctl -u nginx
journalctl -f
journalctl -b
# Analyze boot
systemd-analyze
systemd-analyze blame
Timer Units: Better Than Cron
Timer units offer advantages over traditional cron:
- Persistent timers (run missed events after reboot)
- Randomized delays (prevent thundering herd)
- Calendar syntax with second precision
- Integrated logging via journald
# /etc/systemd/system/backup.timer
[Timer]
OnCalendar=daily
Persistent=true
RandomizedDelaySec=30min
OpenRC: The Elegant Alternative
Dependency-based: Services declare dependencies explicitly (need, use, after, before). Parallel startup within constraints.
# Service control
rc-service nginx start
rc-status
# Enable at boot
rc-update add nginx default
# Runlevels: boot, default, nonetwork, shutdown
Comparison: SysV vs systemd vs OpenRC
| Feature | SysV Init | systemd | OpenRC |
|---|---|---|---|
| Startup | Sequential | Parallel | Dependency-based parallel |
| Boot Time | 30-60s | 10-20s | 15-25s |
| Dependencies | Numeric (S01, S02) | Explicit (After, Requires) | Functions (need, use) |
| Config | Shell scripts | Unit files (INI) | Shell scripts |
| Resource Control | Limited (ulimit) | Full (cgroups) | Basic (cgroups) |
| Logging | Syslog | journald | Syslog |
Advanced systemd Features
Generators: Create units dynamically at boot (e.g., mount units from /etc/fstab).
Drop-in Directories: Override unit settings without modifying originals.
systemctl edit nginx # Creates override in /etc/systemd/system/nginx.service.d/
Template Units: Instantiate multiple services from one template.
# app@.service → app@web.service, app@api.service
systemctl start app@web.service
Resource Control: Limit CPU, memory, and I/O per service using cgroups.
[Service]
CPUQuota=50%
MemoryMax=512M
IOWeight=100
Troubleshooting
# Service fails
systemctl status failed-service
journalctl -u failed-service
# Dependency issues
systemctl list-dependencies service
# Boot analysis
systemd-analyze critical-chain
# Emergency mode
systemctl rescue
Related concepts
Visualize the complete Linux boot sequence from BIOS/UEFI to login. Learn how GRUB, kernel, and systemd work together with interactive visualizations.
Learn how initramfs enables Linux boot by loading essential drivers before the root filesystem mounts. Explore early userspace initialization.
Explore Linux memory management through interactive visualizations. Understand virtual memory, page tables, TLB, swapping, and memory allocation.
Master Linux process management through interactive visualizations. Understand process lifecycle, fork/exec operations, zombies, orphans, and CPU scheduling.
Trace how Btrfs shares extents, creates snapshots, distributes copies, compresses writes, and repairs checksum errors when a good replica exists.
Control groups as instruments: noisy neighbors, v1 vs v2 membership, CFS cpu.max periods, memory.high vs memory.max OOM, and how Docker flags become cgroup files.
