Skip to main content

Chapter 2.6 - Init and systemd Process

Learning Objectives​

By the end of this chapter, you will be able to:

  • Understand the role of the init process (PID 1)
  • Explain how systemd manages services
  • Understand systemd unit files
  • Manage services with systemctl

What is systemd?​

systemd is the initialization and service management system in most modern Linux distributions. It replaces the old init systems (SysV init).


Roles of systemd​

1. First Process (PID 1)​

systemd is always the process with PID 1:

  • Parent of all processes
  • Manages orphan processes
  • Cannot be killed normally

2. Service Management​

systemd starts, stops, and monitors services:

  • Automatic start at boot
  • Restart on failure
  • Dependencies between services
  • State monitoring

3. Runlevel Management​

systemd uses targets (equivalent to runlevels):

  • multi-user.target: Console mode
  • graphical.target: Graphical mode
  • rescue.target: Rescue mode
  • poweroff.target: Shutdown

Unit Files​

systemd services are defined by unit files:

[Unit]
Description=Apache Web Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/sbin/apache2 -D FOREGROUND
Restart=always

[Install]
WantedBy=multi-user.target

Types of unit files:

  • .service: Services
  • .target: Runlevels
  • .timer: Scheduled tasks (alternative to cron)
  • .mount: Mount points
  • .socket: Network sockets

systemctl Commands​

Service management with systemctl:

# View the status of a service
systemctl status apache2

# Start a service
systemctl start apache2

# Stop a service
systemctl stop apache2

# Restart a service
systemctl restart apache2

# Enable at startup
systemctl enable apache2

# Disable at startup
systemctl disable apache2

# View all services
systemctl list-units --type=service

systemd Journal​

systemd manages logs with journalctl:

# View logs of a service
journalctl -u apache2

# Real-time logs
journalctl -f

# Logs from today
journalctl --since today

# Logs with priority
journalctl -p err

Summary​

In this chapter, you learned:

systemd: Modern initialization system, PID 1
Roles: Service, process, runlevel management
Unit files: Service configuration
systemctl: Management commands
journalctl: Log viewing


Next Steps​

Chapter 2.7: Services and Daemons
Chapter 2.8: Runlevels


Chapter created on: December 2024