3

Pretty new to systemctl, I would like to display my VM ip address on console when I boot my centos.

I created a service and enabled it :

[root@centos-3 system]# cat show-ip-on-boot.service
[Unit]
Description=Show IP of eno interface on boot

[Service]
Type=oneshot
ExecStart=/usr/bin/show-ip-on-boot.sh

[Install]
WantedBy=multi-user.target

The "show-ip-on-boot.sh" script is :

 #!/bin/sh

 ip a | grep "inet" | grep "eno" | awk -F/ '{print $1}' | awk '{print $2}'

When I start the service manually, I can see in the logs that it is working :

[root@centos-3 ~]# journalctl -u show-ip-on-boot
-- Logs begin at Thu 2016-10-06 13:59:38 CEST, end at Thu 2016-10-06 14:15:37 CEST. --
Oct 06 14:04:32 centos-3.localdomain systemd[1]: Starting Show IP of eno interface on boot...
Oct 06 14:04:32 centos-3.localdomain show-ip-on-boot.sh[2180]: 192.168.0.43
Oct 06 14:04:32 centos-3.localdomain systemd[1]: Started Show IP of eno interface on boot.

but how can I see it displayed on console at boot ? Should I add something to my script?

Also, when I reboot, I can see in logs that the service is started but it doesn't execute the command :

[root@centos-3 ~]# systemctl status show-ip-on-boot.service
â show-ip-on-boot.service - Show IP of eno interface on boot
Loaded: loaded (/etc/systemd/system/show-ip-on-boot.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Thu 2016-10-06 13:33:55 CEST; 1min 52s ago
Process: 740 ExecStart=/usr/bin/show-ip-on-boot.sh (code=exited, status=0/SUCCESS)
Main PID: 740 (code=exited, status=0/SUCCESS)

Oct 06 13:33:54 centos-3.localdomain systemd[1]: Starting Show IP of eno interface on boot...
Oct 06 13:33:55 centos-3.localdomain systemd[1]: Started Show IP of eno interface on boot.

I probably miss something in systemd concept... Can you please give me a hint on that matter. Thanks.

2 Answers 2

2

systemd collects output from services and logs it in the journal (because this is usually what you want, so that output from services is persistent).

You can change the behavior for a specific service by setting the StandardOutput and StandardError options, as described in the systemd.exec(5) man page, which says:

StandardOutput=

Controls where file descriptor 1 (STDOUT) of the executed processes is connected to. Takes one of inherit, null, tty, journal, syslog, kmsg, journal+console, syslog+console, kmsg+console or socket.

[...]

journal+console, syslog+console and kmsg+console work in a similar way as the three options above but copy the output to the system console as well.

So in theory, something like this should do the trick:

[Service]
Type=oneshot
ExecStart=/usr/bin/show-ip-on-boot.sh
StandardOutput=journal+console
1
  • Thanks for your answer. It displays on console now the ip but only when I start manually the service. However I can see in the logs that the service starts well on boot but it doesn't execute the script (I can't see the ip in the logs). Any idea?
    – Pozinux
    Oct 7, 2016 at 7:27
1

Don't need a service Symply add a line at /etc/issue file. Below my /etc/issue file:

\S

Kernel \r on an \m

My Ip address: \4{enp0s3} <----- Line added,

Working on Centos 7 in a VirtualBox.

The "enp03s" is the network interface card I want to know the IP.

1
  • 1
    Hello and welcome to the U&L stack exchange site! Please review the Help Center to get information on how to best post to this site. To get to your answer, it looks like you are trying to comment on this post. You can make comments on this site once you have high enough reputation. Please edit your post to be a unique answer to this question or ask your own question. Thank you!
    – kemotep
    Mar 12, 2019 at 19:22

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.