How to Optimise Systemd Service in Linux Server

By Raman Kumar

Updated on Sep 18, 2024

In this tutorial, we'll explain how to optimise Systemd service in Linux server.

Systemd is the init system used in most modern Linux distributions, designed for booting and managing services during runtime. Proper management and tuning of systemd services can significantly enhance server performance by ensuring efficient resource allocation and reducing unnecessary overhead. This guide explores how to optimize server performance through systemd service tuning.

Prerequisites

Optimise Systemd Service in Linux Server

1. Understanding Systemd Services

Systemd manages services using unit files, which are configurations for services, devices, timers, and more. Services are primarily handled by .service files that define how a service starts, stops, and interacts with the system.

Key Concepts:

  • Service Units: .service files, e.g., nginx.service, describe how services behave.
  • Target Units: Represent groups of services, similar to runlevels, e.g., multi-user.target.
  • Journal: Systemd logs are stored in the journal, accessed via journalctl.

2. Basic Service Management

Before diving into optimization, familiarize yourself with basic service management commands.

# Start a service
sudo systemctl start service_name

# Stop a service
sudo systemctl stop service_name

# Enable a service to start on boot
sudo systemctl enable service_name

# Disable a service from starting on boot
sudo systemctl disable service_name

# Check service status
sudo systemctl status service_name

3. Optimizing Startup Services

Reducing the number of services that start at boot can significantly reduce boot time and memory consumption.

List All Enabled Services:

systemctl list-unit-files --type=service --state=enabled

Disable Unnecessary Services: Identify non-critical services and disable them:

sudo systemctl disable service_name

Common services that can be disabled on a web server might include bluetooth.service or cups.service (for printing).

4. Setting Service Limits

To prevent resource hogging by individual services, you can limit the amount of CPU, memory, and I/O resources they can use. This is especially useful for resource-intensive services like databases or web servers.

Example: Limiting CPU and Memory

Open the service's unit file for editing

sudo systemctl edit --full service_name

Add or modify resource limits under the [Service] section

[Service]
CPUQuota=50%       # Limit service to 50% of CPU
MemoryLimit=500M   # Limit service to 500MB of RAM

After making changes, reload the service:

sudo systemctl daemon-reload
sudo systemctl restart service_name

5. Managing Service Dependencies

Systemd allows you to define dependencies between services, ensuring that services start and stop in the right order. This can prevent bottlenecks and ensure optimal resource use.

Example: Delaying Service Startup

For non-critical services, you can delay their startup to ensure critical services get more resources during boot.

Open the unit file for editing

sudo systemctl edit --full service_name

Add or modify the following in the [Unit] section

[Unit]
After=network.target  # Start this service after the network is online

This ensures that the service only starts after the network is fully initialized.

6. Leveraging Systemd Timers to Replace Cron Jobs

Systemd timers can be used to run tasks periodically, similar to cron jobs, but they offer more flexibility and reliability.

Create a Service File: Create a service that runs your task.

sudo nano /etc/systemd/system/mytask.service

Add the following content:

[Unit]
Description=My custom task

[Service]
ExecStart=/path/to/script.sh

Create a Timer File: Create a timer that runs the service at defined intervals.

sudo nano /etc/systemd/system/mytask.timer

Add the following:

[Unit]
Description=Runs my custom task every day

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

Enable and Start the Timer:

sudo systemctl enable --now mytask.timer

7. Using Systemd for Resource Accounting

Systemd provides built-in accounting to track resource usage by services, helping identify bottlenecks or misbehaving processes.

Enable Resource Accounting:

Edit the system configuration file to enable resource accounting.

sudo nano /etc/systemd/system.conf

Enable accounting options by modifying or adding the following lines:

DefaultCPUAccounting=yes
DefaultMemoryAccounting=yes
DefaultIOAccounting=yes

Reload systemd and restart the services for the changes to take effect:

sudo systemctl daemon-reload
sudo systemctl restart service_name

Monitor Resource Usage:

You can check resource usage by individual services with:

systemctl status service_name -l

For more detailed information, use the systemd-cgtop command to get a real-time overview of CPU, memory, and I/O usage by systemd units.

systemd-cgtop

8. Optimizing Service Restart Behavior

Systemd allows fine-grained control over how services handle failures and restarts. By tuning these settings, you can improve server stability and avoid resource exhaustion.

Example: Configuring Restart Limits

Edit the service file and add the following:

[Service]
Restart=on-failure          # Automatically restart if the service fails
RestartSec=10               # Wait 10 seconds before restarting
StartLimitIntervalSec=600   # Monitor for 10 minutes
StartLimitBurst=5           # Allow up to 5 restarts in the monitoring window

This configuration restarts a service on failure but prevents it from entering an infinite restart loop.

9. Using Systemd Journal for Performance Monitoring

Systemd logs (journal) are a great tool for tracking service performance, identifying slow startups, and diagnosing failures.

Monitor Boot Times. To analyze boot performance, use:

systemd-analyze blame

This command lists services by startup time, helping you identify performance bottlenecks.

Analyze Service Logs. View detailed logs for a service using journalctl:

journalctl -u service_name

You can filter logs by date and time or keywords to troubleshoot issues that impact performance.

Conclusion

We have seen how to optimise Systemd service in Linux server. By properly managing and optimizing systemd services, you can significantly improve Linux server performance. Disabling unnecessary services, configuring resource limits, managing service dependencies, and leveraging systemd’s logging and monitoring features can help ensure your server operates efficiently and reliably.

Taking the time to fine-tune systemd services not only enhances performance but also improves server stability, making it an essential practice for any Linux server administrator.

With these techniques, you'll be able to manage and optimize your Linux server's performance effectively, ensuring it runs smoothly even under demanding conditions.