Skip to content

Service Management

Prerequisites

  • SSH access to the target machine
  • Root or sudo privileges

Quick Reference

Action Command
List running systemctl list-units --type=service
List all systemctl list-unit-files --type=service
Status systemctl status <svc>
Logs journalctl -u <svc>
Follow logs journalctl -u <svc> -f
Start systemctl start <svc>
Stop systemctl stop <svc>
Restart systemctl restart <svc>
Enable (boot) systemctl enable <svc>
Reload config systemctl daemon-reload

Procedure: List and Filter Services

When to use: Finding out what services exist on the system, which are running, and which are enabled.

Steps:

  1. List all running services:

    systemctl list-units --type=service
    

  2. List all services (including inactive/disabled):

    systemctl list-unit-files --type=service
    

  3. Filter by name:

    systemctl list-unit-files --type=service | grep <keyword>
    

  4. Check if a specific service is active:

    systemctl is-active <service_name>
    

  5. Check if a specific service is enabled on boot:

    systemctl is-enabled <service_name>
    

Troubleshooting:

  • If a service you installed doesn't appear: Run systemctl daemon-reload to rescan unit files.

Procedure: Check Service Status and Logs

When to use: A service failed to start, crashed, or is behaving unexpectedly.

Steps:

  1. Check current status and recent log lines:

    systemctl status <service_name>
    
    Look for Active: active (running) or failed.

  2. View full logs for a specific service:

    journalctl -u <service_name>
    

  3. View live logs (follow mode):

    journalctl -u <service_name> -f
    

  4. View logs from the current boot session only:

    journalctl -u <service_name> -b
    

Troubleshooting:

  • If logs are truncated: Use journalctl -u <service_name> --no-pager or piping to less.
  • If no logs appear: Ensure the service is actually logging to stdout/stderr or syslog.

Procedure: Start/Stop/Restart a Service

When to use: You need to start a daemon, stop a misbehaving service, or apply configuration changes (requires restart).

Steps:

  1. Start a service immediately:

    systemctl start <service_name>
    

  2. Stop a running service:

    systemctl stop <service_name>
    

  3. Restart a service (stop then start):

    systemctl restart <service_name>
    

  4. Reload configuration without interrupting connections (if supported):

    systemctl reload <service_name>
    

  5. Verify the status:

    systemctl status <service_name>
    

Troubleshooting:

  • If systemctl hangs: Check if the service is stuck in a loop or waiting for resources.
  • If "Unit not found": Check spelling with systemctl list-unit-files | grep <name>.

Procedure: Enable a Service on Boot

When to use: Ensure a service starts automatically when the server reboots (e.g., web server, database).

Steps:

  1. Enable the service:

    systemctl enable <service_name>
    
    Note: This usually creates a symlink in /etc/systemd/system/multi-user.target.wants/.

  2. Disable the service (prevent autostart):

    systemctl disable <service_name>
    

  3. Enable and Start in one command:

    systemctl enable --now <service_name>
    

  4. Verify enabled status:

    systemctl is-enabled <service_name>
    

Troubleshooting:

  • If a service is enabled but fails to start on boot: Check journalctl -u <service_name> -b to see logs from the current boot.

Procedure: Create a Custom systemd Unit File

When to use: You want to run a custom script or application as a managed service.

Steps:

  1. Create a unit file at /etc/systemd/system/myapp.service:

    [Unit]
    Description=My Custom Application
    After=network.target
    
    [Service]
    Type=simple
    User=myuser
    ExecStart=/usr/bin/python3 /opt/myapp/main.py
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    

  2. Reload systemd to recognize the new file:

    systemctl daemon-reload
    

  3. Start and enable the service:

    systemctl enable --now myapp
    

Troubleshooting:

  • If modifications aren't picked up: Did you run systemctl daemon-reload?
  • If Exec format error: Ensure ExecStart uses absolute paths (e.g., /usr/bin/python3, not just python3).

  • Technologies: systemd
  • Concepts: Configuration Management