CodexBloom - Programming Q&A Platform

Ubuntu 22.04: Systemd service scenarios to restart on dependency failure despite 'PartOf' configuration

πŸ‘€ Views: 48 πŸ’¬ Answers: 1 πŸ“… Created: 2025-08-30
linux systemd ubuntu bash

I'm performance testing and I'm trying to implement Hey everyone, I'm running into an issue that's driving me crazy... I have a systemd service that I need to ensure restarts automatically if it fails, but it seems to ignore the dependency configuration that I've set. My service, named `my_service.service`, has a dependency on another service called `my_dependency.service`. I configured `my_service.service` with the following settings: ```ini [Unit] Description=My Custom Service PartOf=my_dependency.service Requires=my_dependency.service After=my_dependency.service [Service] ExecStart=/usr/local/bin/my_service_executable Restart=on-failure [Install] WantedBy=multi-user.target ``` The scenario arises when `my_dependency.service` fails to start for some reason. When I check the status of `my_service.service` using `systemctl status my_service.service`, the output shows `Active: failed (Result: dependency)`, but it does not restart as I expected. I've tried using `systemctl reset-failed` to clear the failed state and then starting `my_service.service` again, but that doesn't solve the question. I also checked the logs with `journalctl -u my_service.service` and found entries indicating it fails due to `my_dependency.service` being inactive. Here’s the relevant log snippet: ``` Oct 01 12:34:56 hostname my_service[1234]: Failed to start my_dependency.service: Unit my_dependency.service not found. Oct 01 12:34:56 hostname systemd[1]: my_service.service: Failed to start: dependency. ``` I've tried different combinations of `After=` and `Requires=` and even `BindsTo=` but the behavior remains the same. Is there a way to ensure that `my_service.service` restarts automatically when `my_dependency.service` fails, or is my understanding of systemd dependencies incorrect? Any insights on how to resolve this would be greatly appreciated. I'm working on a CLI tool that needs to handle this. I appreciate any insights! This is happening in both development and production on Windows 11. Is there a simpler solution I'm overlooking?