systemd: service exits immediately after starting

Fix a systemd service that starts and immediately exits due to wrong Type= directive or foreground mismatch.

systemd: service exits immediately after starting

systemd marks a service as failed when the main process exits immediately after starting, usually due to a Type= mismatch.

When systemd Produces This Error

systemd reports a service as failed (or shows Active: inactive (dead)) when the process started by ExecStart exits within seconds of launch. The service appears to start, then immediately stops.

What Causes a Service to Exit Immediately in systemd

The most common cause is a Type= mismatch. If the application forks to the background (daemonizes) but the unit file uses Type=simple, systemd tracks the parent process. The parent exits after forking, and systemd interprets this as the service stopping.

Another cause: the application fails silently — it encounters a configuration error, logs the error to its own log file (not stdout), and exits with code 0. systemd sees a clean exit and does not mark it as failed, but the service is not running.

How to Fix a Service That Exits Immediately in systemd

  1. Check the Type= directive in the unit file. Use Type=forking for applications that daemonize:

    [Service]
    Type=forking
    PIDFile=/run/myapp/myapp.pid
    ExecStart=/opt/myapp/bin/myapp --daemon

    Use Type=simple (default) for applications that run in the foreground. Configure the application to stay in the foreground if possible — this is the preferred approach for systemd.

  2. Check the application's own log files for startup errors that are not captured by the journal.

  3. Run the application manually in the foreground to verify it does not exit:

    sudo -u myapp /opt/myapp/bin/myapp --no-daemon

How to Verify the Fix

systemctl status should show Active: active (running) and the process should remain running for more than a few seconds.