EXIT_CODE=1

systemd service fails with exit code 1: missing env var fix

Linux & Unix Intermediate 👁 23 views 📅 May 26, 2026

Your systemd service exits with code 1 because the environment variable it needs at startup isn't set. Fix by defining env in the service file or unit override.

Quick answer

Add Environment=YOUR_VAR=value or EnvironmentFile=/path/to/file inside the [Service] block of the unit file, then run sudo systemctl daemon-reload && sudo systemctl restart your-service.

Why this happens

When you run a systemd service, it starts in a clean environment — no shell profile, no .bashrc, no .profile. The service's ExecStart process inherits only what systemd explicitly passes. If your app expects something like DATABASE_URL, API_KEY, or HOME to be set, and it isn't, the process exits with code 1. You'll see this in systemctl status your-service or journalctl -u your-service — the logs show a crash or a clear error like “undefined variable” or “missing configuration”. I've seen this most often with Node.js apps, Python scripts, and Docker containers where the app reads environment variables for configuration.

Fix steps

  1. Identify the missing variable
    Run sudo journalctl -u your-service -n 50 --no-pager. Look for error lines like KeyError: 'DB_HOST' or Cannot read property 'PORT' of undefined. That's your missing variable.
    sudo journalctl -u myapp -n 20

    If you don't see a clear error, check your app's documentation for required environment variables.

  2. Edit the service unit
    Use sudo systemctl edit your-service to create an override snippet (don't touch the original unit file directly — it gets overwritten on package updates). This opens a blank .conf file. Paste this:
    [Service]
    Environment=YOUR_VAR=the_value

    If you have multiple variables, list them like this:

    [Service]
    Environment=DB_HOST=localhost
    Environment=DB_PORT=5432
    Environment=API_KEY=abc123

    Alternatively, use an EnvironmentFile to keep secrets out of the unit file — just list variables in a separate file, one per line, e.g. /etc/myapp.env. Then:

    [Service]
    EnvironmentFile=/etc/myapp.env
  3. Reload and restart
    sudo systemctl daemon-reload
    sudo systemctl restart your-service
  4. Verify it's fixed
    sudo systemctl status your-service

    You should see active (running) and no exit code 1.

Alternative fixes if step 2 doesn't work

  • Check if the variable is actually read from a config file — some apps load env vars from .env files, but systemd's working directory isn't your home directory. Set WorkingDirectory=/path/to/app in the [Service] section.
  • Use a wrapper script — if the app needs complex variable setup, write a shell script that sources a file and then runs the binary. Point ExecStart at that script instead. This is hacky but works when you can't control the unit file.
  • Check for typos — I've spent 30 minutes debugging a missing variable only to find I typed ENVIROMENT instead of Environment. Systemd won't warn you — it just ignores the typo.

Prevention tip

Always test your service with sudo systemd-run --unit=test --service-type=simple /path/to/your/app to see if it crashes in a clean environment. That simulates what systemd gives you. Also, avoid relying on shell profile variables in any service — put them in an EnvironmentFile from day one. If you're using Ansible or Puppet to deploy services, include the environment file creation as a separate task so you never forget it.

“The environment is not your shell. Treat it like a new container every time.” — that's how I think about systemd services now. It saves you hours.

For a full list of variables your app needs, run strings /path/to/binary | grep -E '^[A-Z_]+=' if it's a compiled binary, or check the app's README. No guesswork.

Was this solution helpful?