How to create a custom service unit with systemd

Create a custom systemd service unit file to run any application as a managed Linux service.

How to create a custom service unit with systemd

Create a custom systemd service unit file to run any application as a managed Linux service.

Prerequisites

  • Root or sudo access on a Linux system running systemd (Ubuntu 16.04+, Debian 8+, RHEL 7+, Fedora 15+, Arch Linux).
  • The application binary or script to run as a service.

Step-by-Step: Create a Custom systemd Service Unit

  1. Create a new unit file under /etc/systemd/system/. systemd reads custom unit files from this directory with the highest priority:

    sudo nano /etc/systemd/system/myapp.service
  2. Add the unit file content with three required sections. The [Unit] section describes the service and its dependencies. The [Service] section defines how systemd starts and monitors the process. The [Install] section controls boot-time activation:

    [Unit]
    Description=My Custom Application
    After=network.target
    
    [Service]
    Type=simple
    User=myapp
    Group=myapp
    WorkingDirectory=/opt/myapp
    ExecStart=/opt/myapp/bin/myapp --config /etc/myapp/config.yaml
    Restart=on-failure
    RestartSec=5
    StandardOutput=journal
    StandardError=journal
    
    [Install]
    WantedBy=multi-user.target

    The Type=simple directive tells systemd the process started by ExecStart is the main process. Restart=on-failure restarts the service if it exits with a non-zero code. After=network.target ensures the network is available before the service starts.

  3. Reload the systemd daemon to recognize the new unit file:

    sudo systemctl daemon-reload
  4. Enable and start the service:

    sudo systemctl enable --now myapp.service

How to Verify the Custom Service Is Running

systemctl confirms the service state, PID, and recent log output:

systemctl status myapp.service
● myapp.service - My Custom Application
     Loaded: loaded (/etc/systemd/system/myapp.service; enabled)
     Active: active (running) since Mon 2026-03-30 10:15:22 UTC; 1min ago
   Main PID: 4521 (myapp)

Common Issues When Creating Custom systemd Units

The service may fail with status=203/EXEC if the ExecStart path is incorrect or the binary lacks execute permissions. Verify the path exists and is executable with ls -la /opt/myapp/bin/myapp.

The service may exit immediately if the application daemonizes (forks to background) but the unit file uses Type=simple. Use Type=forking for applications that daemonize, or configure the application to run in the foreground.

Forgetting sudo systemctl daemon-reload after editing a unit file causes systemd to use the old configuration. Always run daemon-reload before starting or restarting a modified service.