Article 5NPVD Make systemd change hostname without logging out

Make systemd change hostname without logging out

by
JuventusFC
from LinuxQuestions.org on (#5NPVD)
I have a systemd service that reads some environment values (that may change at startup) and set the hostname accordingly.

The service at the moment is a mix of several tries

Code:[Unit]
Description=Hostname Setting Service
After= read_variables.service
Requires= read_variables.service network-pre.service
Before= systemd-hostnamed.service network-pre.service
DefaultDependencies=false

[Service]
ExecStart=/bin/bash -c "echo name$VARIABLE > /etc/hostname; systemctl start systemd-hostnamed"
StandardOutput=journal+console
EnvironmentFile=/folder/environment_output.txt
Type=oneshot

[Install]
WantedBy=multi-user.targetThe problem is this one:

Code:at n-1-th startup, environment variables are set at n-1-th value
reboot
n-th startup
n-1-th values are read in /etc/hostname and then hostname is set to name@n-1
correct n-th values are read with read_variables.service and put in environment_output
correct hostname is set in /etc/hostname
But hostname is not modified on the fly so I will have hostname@n-1 instead of hostname@nSo my question is how to run these scripts before systemd prints the line:

systemd[1]: Set hostname to XYZlatest?d=yIl2AUoC8zA latest?i=OINoGXdUC3Q:YEqg5rs6xhw:F7zBnMy latest?i=OINoGXdUC3Q:YEqg5rs6xhw:V_sGLiP latest?d=qj6IDK7rITs latest?i=OINoGXdUC3Q:YEqg5rs6xhw:gIN9vFwOINoGXdUC3Q
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments