0

I want to add my script in etc/rc.local file to make it auto start when the machine starts. My script needs a network condition, I found the network is not ready when running this script. In my opinion, the rc.local maybe start in a late-stage, so should the network be ready? Or if it is normal that the network is not available at this stage, where should I put my script? Any comments will be welcome.

my linux machine is on Linux localhost.localdomain 4.18.0-147.8.1.el8_1.x86_64

a piece of code in rc.local: ip address >> /home/user/log.boot 2>&1

log.boot as follows:

eno1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
 linker/ether xxxxxx brd ff:ff:ff:ff:ff:ff
terry
  • 1
  • In case you use systemd, have you seen https://unix.stackexchange.com/questions/126009/cause-a-script-to-execute-after-networking-has-started? – Edward Sep 17 '21 at 09:12
  • It's more about the specific distribution and its default network handling than about "Linux": you should state what is your distribution (and its version) of Linux in the question. – A.B Sep 17 '21 at 09:45

2 Answers2

0

if i'm not wrong the /etc/rc.local is deprecated. if you need run a command after the boot use cron with @reboot special tag.

Create a new file in /etc/cron.d/ and add a new line with

@reboot ip address >> /home/user/log.boot 2>&1

At the next boot cron will run ip address >> /home/user/log.boot 2>&1.

maruscya
  • 71
  • 3
0

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.

telcoM
  • 96,466
  • Thanks for your answer @telcoM, I tried your method, unfortunately, it couldn't work. I still couldn't see any valid IP address output... If I add this script in /etc/profile, it works, but I couldn't want to add it there... – terry Sep 18 '21 at 03:26