The systemd service failed because the systemd supervisor process, systemd, couldn’t find the service’s definition file in its expected locations.
Common Causes and Fixes
1. Missing .service File
- Diagnosis: Check if the
.servicefile exists in the standardsystemdpaths:/etc/systemd/system/,/run/systemd/system/, or/usr/lib/systemd/system/.ls -l /etc/systemd/system/your-service-name.service ls -l /usr/lib/systemd/system/your-service-name.service - Fix: If the file is missing, create it. For example, for a hypothetical
my-app.service:
Add content similar to this:sudo nano /etc/systemd/system/my-app.service
This creates the necessary service definition so[Unit] Description=My Application Service After=network.target [Service] ExecStart=/usr/local/bin/my-app Restart=always User=myuser Group=mygroup [Install] WantedBy=multi-user.targetsystemdknows how to manage your application. - Why it works:
systemdrelies on these.servicefiles to understand how to start, stop, and manage processes. Without the file, it has no instructions.
2. Incorrect File Permissions
- Diagnosis: Even if the file exists,
systemdneeds read access. Check permissions:
The typical correct permissions arels -l /etc/systemd/system/my-app.servicerw-r--r--(644). - Fix: If permissions are too restrictive, set them correctly:
This ensures thesudo chmod 644 /etc/systemd/system/my-app.servicesystemdprocess can read the service definition. - Why it works:
systemdruns as a specific user (often root) and needs to be able to read the service configuration files to load them.
3. Incorrect File Ownership
- Diagnosis: The file should typically be owned by
root:root.ls -l /etc/systemd/system/my-app.service - Fix: If ownership is incorrect, change it:
This aligns the file’s ownership with the usersudo chown root:root /etc/systemd/system/my-app.servicesystemdruns as, ensuring proper access. - Why it works: Similar to permissions,
systemdneeds to access the file, and correct ownership is a prerequisite for that access.
4. Typos in the .service File
- Diagnosis: A simple typo in the
[Unit],[Service], or[Install]sections can causesystemdto reject the file. Usesystemd-analyze verifyto check the syntax:sudo systemd-analyze verify /etc/systemd/system/my-app.service - Fix: Correct any reported syntax errors in the
.servicefile. For example, ifExecStartwas misspelled asExecStrart:
This ensures# Incorrect ExecStrart=/usr/local/bin/my-app # Correct ExecStart=/usr/local/bin/my-appsystemdcan parse the file correctly. - Why it works:
systemdhas a specific format for its unit files. Any deviation, even a minor typo, prevents it from interpreting the configuration.
5. Incorrect WantedBy or Requires Directives
- Diagnosis: The
[Install]section’sWantedBydirective tellssystemdwhich target to link the service to whensystemctl enableis run. If this is incorrect or points to a non-existent target, enabling will fail.grep "WantedBy" /etc/systemd/system/my-app.service - Fix: Ensure
WantedBypoints to a valid target, commonlymulti-user.targetfor services that should start at boot in a multi-user environment.
Then, try enabling the service again:[Install] WantedBy=multi-user.target
This correctly registers the service with the desired system state.sudo systemctl enable my-app.service - Why it works: The
WantedBydirective is crucial forsystemctl enable. It creates symbolic links in the target’s.wantsdirectory, making the service start automatically when that target is activated.
6. Service File Not Reloaded After Changes
- Diagnosis: If you’ve made changes to a
.servicefile butsystemdis still using the old configuration, you’ll see this error. - Fix: After modifying any
.servicefile, always tellsystemdto reload its configuration:
Then, try starting or enabling your service again.sudo systemctl daemon-reload - Why it works:
systemdcaches its configuration.daemon-reloadforces it to re-read all unit files from disk, picking up your recent changes.
7. File Placed in the Wrong Directory
- Diagnosis: While less common, placing the
.servicefile in a directory not scanned bysystemdwill lead to this error. The primary directories are/etc/systemd/system/(for administrator-created units),/run/systemd/system/(for runtime-generated units), and/usr/lib/systemd/system/(for distribution-provided units). - Fix: Move the
.servicefile to the correct location, typically/etc/systemd/system/for custom services.
This places the file wheresudo mv /path/to/your/my-app.service /etc/systemd/system/ sudo systemctl daemon-reloadsystemdexpects to find administrator-managed services. - Why it works:
systemdhas a defined search path for unit files. Placing it in the correct directory ensures it’s discovered during thedaemon-reloadprocess.
After fixing these issues, the next error you’re likely to encounter if your service application itself has problems is a "failed to start" error with a specific exit code or a message indicating the executable could not be found or run.