0

I have a service child.service that depends on parent.service. I would like child.service to start (or restart) any time parent.service is started or restarted.

I have been reading U&L Stack Exchange (as well as the other relevant answers on other Stack Exchange sites) for about an hour, and I cannot get child.service to start or restart when parent.service is restarted.

The parent service starts and runs with no problems (e.g. it is not exiting early). Here's the parent.service config, in /etc/systemd/system/parent.service.d/override.conf:

[Unit]
Description=parent
After=network.target

[Service]
Type=simple
ExecStart=
ExecStart=/usr/bin/parent-start
ExecStartPost=/usr/local/bin/afterparentstart
Restart=always

[Install]
WantedBy=multi-user.target

Here is the child service configuration, in /etc/systemd/user/child.service:

[Unit]
Description=child
Requires=network.target
Wants=nss-lookup.target
Before=nss-lookup.target
After=network.target
Requires=parent.service
PartOf=parent.service
After=parent.service

[Service]
Type=forking
PIDFile=/run/child/child.pid
Restart=always

ExecStart=/usr/local/bin/start-child

# Original file had HUP here but HUP does not trigger closing/reopening the log
# file handle as far as I can tell.
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
WantedBy=parent.service

I can see when I run "systemctl show parent.service" that a ConsistsOf=child.service relationship is present. However, if child.service fails, or I manually stop it, and then I run systemctl restart parent.service, I observe in systemctl status child.service that the child service has not started or attempted to start.

I am using Ubuntu 18.04 with systemd version 237. What am I missing here? I am at wit's end. Thanks, Kevin

0

Aha, I was running systemctl daemon-reload but this wasn't enough. I needed to re-enable the child systemd unit in order to create the right set of symlinks.

$ systemctl reenable /etc/systemd/user/child.service
Created symlink /etc/systemd/system/multi-user.target.wants/child.service → /etc/systemd/user/child.service.
Created symlink /etc/systemd/system/parent.service.wants/child.service → /etc/systemd/user/child.service.
Created symlink /etc/systemd/system/child.service → /etc/systemd/user/child.service.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.