Service Management with Systemd

ยท

3 min read

systemd is more than just a service manager - it's a suite of basic building blocks for a Linux system. However, one of its primary functions is managing services, or daemons, that run in the background. These services can include everything from yoprimaryur SSH server to your database management system.

At the heart of systemd's service management are unit files. These are configuration files that describe how systemd should manage a service. While there are several types of unit files, we'll focus on service units (ending in .service).

A typical service unit file might look like this:

[Unit]
Description=My Custom Web Server
After=network.target
Requires=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/mywebserver
Restart=always
User=webserver

[Install]
WantedBy=multi-user.target

Here are the essential systemd commands for managing services:

# Start a service
sudo systemctl start nginx.service

# Stop a service
sudo systemctl stop nginx.service

# Restart a service
sudo systemctl restart nginx.service

# Reload configuration without stopping
sudo systemctl reload nginx.service

# Enable service to start at boot
sudo systemctl enable nginx.service

# Disable service from starting at boot
sudo systemctl disable nginx.service

# View detailed service status
sudo systemctl status nginx.service

# Check if service is running
sudo systemctl is-active nginx.service

# Check if service is enabled at boot
sudo systemctl is-enabled nginx.service

One of systemd's strengths is its sophisticated dependency management. Unit files can specify:

  • Requires: Hard dependencies that must be active

  • Wants: Optional dependencies

  • After/Before: Ordering requirements

  • Conflicts: Services that cannot run simultaneously

systemd supports various service types:

  • simple: The main process is the service

  • forking: The service forks a child process

  • oneshot: The service exits after completion

  • notify: The service sends a notification when ready

  • idle: The service starts after other jobs complete

systemd provides robust tools for monitoring and troubleshooting services:

# View service logs
journalctl -u nginx.service

# View logs since last boot
journalctl -u nginx.service -b

# Follow logs in real-time
journalctl -u nginx.service -f

Note: The journalctl logs are ordered from chronological order (from old time to current time)

systemd can manage service resource usage:

[Service]
CPUQuota=50%
MemoryLimit=1G
TasksMax=100
  1. Always Use Full Unit Names: Include the .service suffix for clarity.

  2. Utilize Environment Files: Keep configuration separate from unit files.

  3. Set Appropriate Restart Policies: Choose between no, always, on-success, or on-failure.

  4. Implement Resource Limits: Prevent runaway services from impacting system stability.

  5. Document Unit Files: Use comments to explain complex configurations.

  • systemd Official Documentation

  • man systemd.service

  • man systemd.unit

  • man systemctl