0

I have systemd 252 on Debian. I want when the systemd service systemd-hostnamed.service restarts or starts to restart my b.service.

I used with no luck on my b.service the follow options.

After=systemd-hostnamed.service
Requires=systemd-hostnamed.service
BindsTo=systemd-hostnamed.service
PartOf=systemd-hostnamed.service

How can I do that?

b.service

[Unit]
Description=Avoid NetworkManager sending hostname in DHCP request
Wants=network-pre.target
Before=network-pre.target

[Service]
StartLimitBurst=5
StartLimitInterval=80
RestartSec=15
User=root
ExecStart=/usr/bin/python3 /usr/local/bin/hostname.py

[Install]
WantedBy=sysinit.target
1
  • 1
    In systemd-hostnamed.service add Wants=b.service. In b.service keep PartOf=systemd-hostnamed.service.
    – user103944
    Commented Sep 24, 2023 at 17:17

2 Answers 2

0

b.service needs two relationships with systemd-hostnamed.service:

  • PartOf=systemd-hostnamed.service will cause b.service to stop when systemd-hostnamed.service is stopped.
  • WantedBy=systemd-hostnamed.service will cause b.service to start when systemd-hostnamed.service is started.

Both of these are used in a reset.

Adding the PartOf= relationship is trivial. Just add that line to your [Unit] section.

[Unit]
PartOf=systemd-hostnamed.service

WantedBy= can only be defined in the [Install] section, but you already have WantedBy=sysinit.target. If you expect systemd-hostnamed.service to be started on sysinit, then you could just change this line. Then sudo systemctl {disable,enable} b.service.

[Install]
# WantedBy=sysinit.target
WantedBy=systemd-hostnamed.service

If you want to keep b.service's [Install] section unchanged, then an equivalent option is to add Wants=b.service to systemd-hostnamed.service. This is done via overrides. Use sudo systemctl edit systemd-hostnamed.service and set it to the following and save:

$ sudo systemctl edit systemd-hostnamed.service

[Unit]
Wants=b.service

This creates /etc/systemd/system/systemd-hostnamed.service.d/override.conf which adds the relationship to systemd-hostnamed.service without contaminating the original file deployed by systemd.

3
  • Thank you Stewart. I created /etc/systemd/system/systemd-hostnamed.service.d/override.conf and put Wants=b.service . Do I have to add in main b.service Unit section PartOf=systemd-hostnamed.service ? Commented Sep 25, 2023 at 19:02
  • Yes. b.service needs [Unit] PartOf=systemd-hostnamed.service to stop when systemd-hostnamed.service stops. systemd.hostnamed.service.d/override.conf needs [Unit] Wants=b.service to trigger b.service to start. Don't forget the [Unit] header in override.conf.
    – Stewart
    Commented Sep 26, 2023 at 9:11
  • You are God!!! Thank you very much. Please would you like to read one unsolved post here? unix.stackexchange.com/questions/754854/… Commented Sep 26, 2023 at 10:24
0

Wrong layer.

[ipv4]
dhcp-send-hostname=false

in your profile on /etc/NetworkManager/system-connections/ will stop networkmanager from sending a dhcp hostname.

1
  • I know it. App doing this. Commented Sep 30, 2023 at 6:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .