The default definition for the systemd service running /etc/rc.d/rc.local
(which is symlinked to /etc/rc.local
in RHEL/CentOS/UEL 8) is:
[Unit]
Description=/etc/rc.d/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
Note that the only ordering dependency specified is After=network.target
. This does not mean much: basically, it could be executed as soon as NetworkManager.service
and rsyslog.service
have been started. So in practice, it often runs in parallel while NetworkManager is working to bring the network interfaces up, which is too early for your particular purpose.
This would be a good example on writing a minimal systemd service of your own.
First, the systemd ExecStart=
is not a shell command line: it won't support redirection. So, create a minimal shell script for your command, e.g. /usr/local/bin/log-boot-ip.sh
and mark it executable (chmod +x /usr/local/bin/log-boot-ip.sh
):
#!/bin/sh
ip address >> /home/user/log.boot 2>&1
Then let's write a minimal /etc/systemd/system/log-boot-ip.service
:
[Unit]
Description=log IP address(es) at each boot
ConditionFileIsExecutable=/usr/local/bin/log-boot-ip.sh
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/log-boot-ip.sh
[Install]
WantedBy=multi-user.target
As always after adding or modifying systemd
service definitions, run systemctl daemon-reload
after putting the log-boot-ip.service
in place.
Description
: just a human-readable short description
ConditionFileIsExecutable
: will harmlessly skip this service if our script is not executable
Wants
and After
: this is how you set up a service to run after network interfaces have been activated, see man systemd.special
.
Type=oneshot
: because the script will exit without leaving anything running. Without this, systemd would assume Type=simple
and would assume the script exiting would mean it had failed.
RemainAfterExit=true
: makes systemd "remember" that this service was already executed once, and show it as "active" even though the script exited.
ExecStart
: specifies the actual command/script to run.
WantedBy=multi-user.target
: causes the service to be added into the normal boot process.