'Add dependency between two systemd services to start again if one of them is killed
I have two systemd services A.service and B.service in /etc/systemd/system/ directory.
Below is the configuration in A.service
[Unit]
Description=A Service
[Service]
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/opt/myapp/A_service.sh -a start
ExecStop=/opt/myapp/A_service.sh -a stop
[Install]
WantedBy=multi-user.target
Below is the configuration in B.service
[Unit]
Description=B Service
After=A.service
BindsTo=A.service
[Service]
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/opt/myapp/B_service.sh -a start
ExecStop=/opt/myapp/B_service.sh -a stop
[Install]
WantedBy=multi-user.target
I want to create a dependency such that when A.service gets restarted B.service should also get restarted.
But it's not working when i kill A.service with sudo kill -9 <A.service pid>
.
I can see A.service getting restarted after kill but B.service is in stopped state, not getting restarted.
Can someone help me configure this dependency?
Thanks in Advance.
Solution 1:[1]
You want to use the PartOf
dependency.
https://www.freedesktop.org/software/systemd/man/systemd.unit.html#PartOf=
PartOf=
Configures dependencies similar to Requires=, but limited to stopping and restarting of units. When systemd stops or restarts the units listed here, the action is propagated to this unit. Note that this is a one-way dependency — changes to this unit do not affect the listed units. When PartOf=b.service is used on a.service, this dependency will show as ConsistsOf=a.service in property listing of b.service. ConsistsOf= dependency cannot be specified directly.
Description=B Service
After=A.service
BindsTo=A.service
PartOf=A.service
[Service]
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/opt/myapp/B_service.sh -a start
ExecStop=/opt/myapp/B_service.sh -a stop
[Install]
WantedBy=multi-user.target
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Brendan |