Developer Guides

Systemd Service Tutorial: Run Apps as Services on Linux

Learn to manage applications as services on Linux using systemd, ensuring restarts on failure and auto-start at boot.

3 min read

Learn how to configure applications as systemd services on Linux, enabling auto-start at boot, frequent restarts on failure, and centralized logging. This tutorial provides step-by-step instructions to create, manage, and troubleshoot systemd unit files.


Prerequisites for Setting Up a Systemd Service

Before configuring a systemd service, ensure that you have the necessary environment and permissions.

prerequisites

  • A Linux distribution that uses systemd (most modern distributions, e.g., Ubuntu, Fedora, CentOS).
  • Administrator (sudo) access for system-level operations.
  • An installed and configured application or script to manage as a service.

Steps to Create and Manage a Systemd Service

Follow these steps to configure applications as a systemd service effectively:

steps

  1. Log into your Linux system as a user with sudo privileges.
  2. Navigate to the /etc/systemd/system/ directory, the recommended location for custom service files:
    bash
    cd /etc/systemd/system/
  3. Create a new .service file using a text editor:
    bash
    sudo nano my-service.service
  4. Define the unit file structure:
    [Unit]
    Description=My App Service
    After=network.target
    
    [Service]
    ExecStart=/path/to/your/app
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
  5. Save the file and reload the systemd daemon to recognize the new unit:
bash
   sudo systemctl daemon-reload
   # Applies changes to systemd units
  1. Enable the service to start automatically at boot:
bash
   sudo systemctl enable my-service.service
   # Links the service for startup
  1. Start the service manually:
bash
   sudo systemctl start my-service.service
   # Initializes the service
  1. Check the service status to ensure proper operation:
bash
   sudo systemctl status my-service.service
   # Displays service state and logs
  1. (Optional) Disable the service if you no longer want it to run at boot:
bash
   sudo systemctl disable my-service.service
   # Removes the boot link
  1. Remember to reload the systemd daemon after every file edit:
bash
    sudo systemctl daemon-reload
    # Ensures updates to unit files are applied

Common Mistakes and Solutions

Here are common errors and their fixes when managing systemd services:


Verifying Service Functionality

Confirm that your service runs as expected and handles reboots correctly.


FAQ

FAQ

How do I restart a systemd service after configuration changes?

Run sudo systemctl daemon-reload to apply changes, then sudo systemctl restart service-name.service to restart the service.

Can I override default unit files?

Yes, you can override system defaults by creating an override config in /etc/systemd/system/service-name.d/override.conf or using sudo systemctl edit service-name.

How can I debug a failing systemd service?

Use sudo journalctl -u service-name for logs and sudo systemctl status service-name to inspect runtime issues.

How do I automate service restarts on failure?

Set Restart=always in the [Service] section of your .service file to restart automatically.

Do changes to unit file paths require additional commands?

Yes. Always run sudo systemctl daemon-reload after editing unit files to ensure systemd reloads the configurations.


Official reference: systemd.service manual.