Complete Guide to Systemd: Managing Services on Modern Linux

Systemd has become the standard init system and service manager for most Linux distributions. Understanding systemd is essential for any Linux administrator or developer. This comprehensive guide covers everything from basic service management to creating custom unit files and debugging system issues.

Understanding Systemd Architecture

Systemd is more than an init system. It manages system services, logging, network configuration, and much more. At its core are unit files that describe services, mounts, timers, and other system resources.

Essential Systemctl Commands

# Start, stop, restart services
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx

# Enable/disable at boot
sudo systemctl enable nginx
sudo systemctl disable nginx

# Check service status
systemctl status nginx

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

# List failed services
systemctl --failed

Creating Custom Service Files

Custom services are defined in /etc/systemd/system/. Here is a template for a typical application service:

# /etc/systemd/system/myapp.service
[Unit]
Description=My Application Service
After=network.target
Wants=network-online.target

[Service]
Type=simple
User=myapp
Group=myapp
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/start.sh
ExecStop=/opt/myapp/bin/stop.sh
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

Working with Timers

Systemd timers are a modern replacement for cron jobs, offering more flexibility and better integration with the system.

# /etc/systemd/system/backup.timer
[Unit]
Description=Daily Backup Timer

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

# Enable and start timer
sudo systemctl enable --now backup.timer

# List active timers
systemctl list-timers

Journalctl for Log Management

# View all logs
journalctl

# Follow logs in real-time
journalctl -f

# Logs for specific service
journalctl -u nginx

# Logs since last boot
journalctl -b

# Logs from specific time
journalctl --since "2025-01-01" --until "2025-01-02"

# Show only errors
journalctl -p err

Analyzing Boot Performance

# Show boot time
systemd-analyze

# Blame slowest services
systemd-analyze blame

# Generate boot graph
systemd-analyze plot > boot.svg

Resource Control with Cgroups

Systemd uses cgroups to manage and limit resource usage for services.

# Limit CPU usage
[Service]
CPUQuota=50%

# Limit memory
MemoryMax=512M

# IO limits
IOWeight=100

Conclusion

Mastering systemd is essential for modern Linux administration. Its unified approach to service management, logging, and resource control simplifies system management while providing powerful features for complex deployments.

Was this article helpful?

R

About Ramesh Sundararamaiah

Red Hat Certified Architect

Expert in Linux system administration, DevOps automation, and cloud infrastructure. Specializing in Red Hat Enterprise Linux, CentOS, Ubuntu, Docker, Ansible, and enterprise IT solutions.

🐧 Stay Updated with Linux Tips

Get the latest tutorials, news, and guides delivered to your inbox weekly.

Add Comment