Troubleshooting and Best Practices
In this final step, you will learn about common issues that can occur when working with systemd services and how to troubleshoot them. You will also learn some best practices for working with systemctl daemon-reload
.
Troubleshooting common issues
1. Service fails to start
If your service fails to start, the first step is to check its status:
systemctl status hello-service
This often provides detailed error information. Let's intentionally create an error to see how to troubleshoot it:
- Edit the service file:
sudo nano /etc/systemd/system/hello-service.service
- Change the
ExecStart
line to point to a non-existent script:
ExecStart=/home/labex/project/scripts/non-existent.sh
-
Save and exit the editor.
-
Reload the systemd configuration and try to restart the service:
sudo systemctl daemon-reload
sudo systemctl restart hello-service
- Check the status to see the error:
systemctl status hello-service
You should see error messages indicating that the script doesn't exist:
● hello-service.service - Hello Service Demo
Loaded: loaded (/etc/systemd/system/hello-service.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since [timestamp]; [time] ago
Process: [pid] ExecStart=/home/labex/project/scripts/non-existent.sh (code=exited, status=203/EXEC)
Main PID: [pid] (code=exited, status=203/EXEC)
CPU: 5ms
[timestamp] systemd[1]: Started Hello Service Demo.
[timestamp] systemd[pid]: hello-service.service: Failed to execute command: No such file or directory
[timestamp] systemd[pid]: hello-service.service: Failed at step EXEC spawning /home/labex/project/scripts/non-existent.sh: No such file or directory
[timestamp] systemd[1]: hello-service.service: Main process exited, code=exited, status=203/EXEC
[timestamp] systemd[1]: hello-service.service: Failed with result 'exit-code'.
- Fix the service file to point back to the correct script:
sudo nano /etc/systemd/system/hello-service.service
- Change the
ExecStart
line back to:
ExecStart=/home/labex/project/scripts/hello-service.sh
- Save and exit, then reload and restart:
sudo systemctl daemon-reload
sudo systemctl restart hello-service
- Verify the service is running again:
systemctl status hello-service
When troubleshooting, you can use journalctl
to view detailed logs:
sudo journalctl -u hello-service
This shows all logs for the hello-service unit, which can be invaluable for diagnosing issues.
To see only the most recent logs:
sudo journalctl -u hello-service -n 20
To follow the logs in real-time (similar to tail -f
):
sudo journalctl -u hello-service -f
Press Ctrl+C
to exit the log following mode.
Best practices for working with systemctl daemon-reload
Here are some best practices to follow when working with systemd and the daemon-reload
command:
-
Always reload after changes: Always run systemctl daemon-reload
after modifying, adding, or removing any systemd unit files.
-
Check syntax before applying: You can use the following command to check for syntax errors in your service files before reloading:
sudo systemd-analyze verify /etc/systemd/system/hello-service.service
If there are no errors, this command produces no output. Any syntax errors will be reported.
-
Use service templates: For services that require multiple instances, use service templates (files named like service@.service
) to avoid duplication.
-
Review service dependencies: Make sure your services have the correct After=
and Requires=
directives to ensure they start in the correct order.
-
Clean up properly: When removing a service, make sure to stop and disable it before deleting the service file:
sudo systemctl stop hello-service
sudo systemctl disable hello-service
sudo rm /etc/systemd/system/hello-service.service
sudo systemctl daemon-reload
Let's clean up our example service now:
sudo systemctl stop hello-service
sudo systemctl disable hello-service
You should see output indicating that the symlink was removed:
Removed /etc/systemd/system/multi-user.target.wants/hello-service.service.
We won't delete the service file yet as we still need it for verification.
Now you understand how to create, modify, and troubleshoot systemd services, as well as the importance of the systemctl daemon-reload
command in managing these services.