You are not logged in.

#1 2022-08-14 09:13:58

topasiss
Member
Registered: 2022-08-08
Posts: 50

systemd user service: Unable to configure service after network-online

Hello everyone.

I want to start a systemd user service after the network connection has an IP address.

I followed systemd - Running services after the network is up. (I use NetworkManager)
I set up the following service:

[tobiasb@tobias-pc ~]$ cat ~/.config/systemd/user/olad.service 
[Unit]
Description=Open Lighting Architecture service (olad)
# Doesn't work so far, might be because dhcp quits getting a lease caused by timeout, see discussions in google groups
Wants=network-online.target
After=network-online.target
[Service]
# Dirty workaround for issue https://github.com/OpenLightingProject/ola/issues/1546
#ExecStartPre = /bin/sleep 5
ExecStart=/usr/bin/olad --log-level 3
[Install]
WantedBy=default.target

I have checked that the required services for the network-online.target are enabled. (They were enabled - I did'nt change their state)

[tobiasb@tobias-pc ~]$ systemctl is-enabled NetworkManager-wait-online.service NetworkManager.service
enabled
enabled

According to systemd - How it works:

User units can not reference or depend on system units or units of other users.

So the

Wants=network-online.target
After=network-online.target

in the service file won't work in user services.
How to get around this is something I didn't find on the wiki. But I found a hint in the arch forums and applied it by:

systemctl --user link /usr/lib/systemd/system/network-online.target

Since the network-online.target does not wait for the network to be online, but only for the network manager is in a good state I overrode the network online wait service as described on many places on the net and in the arch user forums (See override section at the bottom):

[tobiasb@tobias-pc ~]$ sudo systemctl cat NetworkManager-wait-online.service
# /etc/systemd/system/NetworkManager-wait-online.service
[Unit]
Description=Network Manager Wait Online
Documentation=man:nm-online(1)
Requires=NetworkManager.service
After=NetworkManager.service
Before=network-online.target

[Service]
# `nm-online -s` waits until the point when NetworkManager logs
# "startup complete". That is when startup actions are settled and
# devices and profiles reached a conclusive activated or deactivated
# state. It depends on which profiles are configured to autoconnect and
# also depends on profile settings like ipv4.may-fail/ipv6.may-fail,
# which affect when a profile is considered fully activated.
# Check NetworkManager logs to find out why wait-online takes a certain
# time.

Type=oneshot
ExecStart=/usr/bin/nm-online -s -q
RemainAfterExit=yes

# Set $NM_ONLINE_TIMEOUT variable for timeout in seconds.
# Edit with `systemctl edit NetworkManager-wait-online`.
#
# Note, this timeout should commonly not be reached. If your boot
# gets delayed too long, then the solution is usually not to decrease
# the timeout, but to fix your setup so that the connected state
# gets reached earlier.
Environment=NM_ONLINE_TIMEOUT=60

[Install]
WantedBy=network-online.target

# /etc/systemd/system/NetworkManager-wait-online.service.d/override.conf
[Service]
ExecStart=
ExecStart=/usr/bin/nm-online -q

Now the network online target is reached before the service is started, but the network connection is established long after the service has started and I cannot find out why.
I attached some filtered logs of the startup process where one can observe the finished network-online.target before the service in question (olad) gets started and NetworkManager getting in the connected state after network online target be reached:

sudo journalctl --boot -0 --grep=olad\|target\|NetworkManager

[...]

Aug 14 10:15:44 tobias-pc systemd[1]: Reached target Graphical Interface.
Aug 14 10:15:44 tobias-pc systemd[1]: Reached target Sound Card.
Aug 14 10:15:44 tobias-pc NetworkManager[371]: <info>  [1660464944.5902] Loaded device plugin: NMBluezManager (/usr/lib/NetworkManager/1.38.2-2/libnm-de>
Aug 14 10:15:44 tobias-pc NetworkManager[371]: <info>  [1660464944.5908] Loaded device plugin: NMAtmManager (/usr/lib/NetworkManager/1.38.2-2/libnm-devi>
Aug 14 10:15:44 tobias-pc NetworkManager[371]: <info>  [1660464944.5919] Loaded device plugin: NMWifiFactory (/usr/lib/NetworkManager/1.38.2-2/libnm-dev>
Aug 14 10:15:44 tobias-pc NetworkManager[371]: <info>  [1660464944.5974] Loaded device plugin: NMTeamFactory (/usr/lib/NetworkManager/1.38.2-2/libnm-dev>
Aug 14 10:15:44 tobias-pc NetworkManager[371]: <info>  [1660464944.5981] Loaded device plugin: NMOvsFactory (/usr/lib/NetworkManager/1.38.2-2/libnm-devi>
Aug 14 10:15:44 tobias-pc NetworkManager[371]: <info>  [1660464944.5985] Loaded device plugin: NMWwanFactory (/usr/lib/NetworkManager/1.38.2-2/libnm-dev>
Aug 14 10:15:44 tobias-pc dbus-daemon[304]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm->
Aug 14 10:15:44 tobias-pc NetworkManager[371]: <info>  [1660464944.6026] manager: (lo): new Generic device (/org/freedesktop/NetworkManager/Devices/1)
Aug 14 10:15:44 tobias-pc NetworkManager[371]: <info>  [1660464944.6045] manager: (enp5s0): new Ethernet device (/org/freedesktop/NetworkManager/Devices>
Aug 14 10:15:44 tobias-pc audit[1]: SERVICE_START pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=NetworkManager-dispatcher comm="systemd" exe="/us>
Aug 14 10:15:44 tobias-pc systemd[403]: Queued start job for default target Main User Target.
Aug 14 10:15:44 tobias-pc systemd[403]: Reached target Network is Online.
Aug 14 10:15:44 tobias-pc systemd[403]: Reached target Paths.
Aug 14 10:15:44 tobias-pc systemd[403]: Reached target Timers.
Aug 14 10:15:44 tobias-pc olad[460]: olad/Olad.cpp:98: OLA Daemon version 0.10.8
Aug 14 10:15:44 tobias-pc olad[460]: olad/OlaDaemon.cpp:121: Using configs in /home/tobiasb/.ola
Aug 14 10:15:44 tobias-pc olad[460]: olad/OlaServer.cpp:186: No network interface found
Aug 14 10:15:44 tobias-pc olad[460]: olad/OlaServer.cpp:194: Server UID is 7a70:00000000
Aug 14 10:15:44 tobias-pc olad[460]: olad/OlaServer.cpp:206: Server instance name is OLA Server
Aug 14 10:15:44 tobias-pc olad[460]: olad/AvahiDiscoveryAgent.cpp:201: Client state changed to AVAHI_CLIENT_S_REGISTERING
Aug 14 10:15:44 tobias-pc olad[460]: olad/OlaServer.cpp:481: Updated PID definitions.
Aug 14 10:15:44 tobias-pc olad[460]: olad/OlaServer.cpp:489: PID store is at 0x5594435a4130

[...]

Aug 14 10:15:44 tobias-pc olad[460]: olad/PluginManager.cpp:195: Trying to start E1.31 (sACN)
Aug 14 10:15:44 tobias-pc olad[460]: olad/PluginManager.cpp:198: Failed to start E1.31 (sACN)
Aug 14 10:15:44 tobias-pc olad[460]: olad/PluginManager.cpp:195: Trying to start FTDI USB DMX
Aug 14 10:15:44 tobias-pc olad[460]: olad/plugin_api/DeviceManager.cpp:105: Installed device: FT232R USB UART with serial number : A9014PL1 :13-A9014PL1
Aug 14 10:15:44 tobias-pc olad[460]: olad/plugin_api/PortManager.cpp:119: Patched 13-A9014PL1-O-1 to universe 1
Aug 14 10:15:44 tobias-pc olad[460]: olad/PluginManager.cpp:200: Started FTDI USB DMX
Aug 14 10:15:45 tobias-pc NetworkManager[371]: <info>  [1660464945.0325] manager: (wlan0): new 802.11 Wi-Fi device (/org/freedesktop/NetworkManager/Devi>
Aug 14 10:15:45 tobias-pc olad[460]: olad/AvahiDiscoveryAgent.cpp:201: Client state changed to AVAHI_CLIENT_S_RUNNING
Aug 14 10:15:45 tobias-pc olad[460]: olad/AvahiDiscoveryAgent.cpp:236: State for OLA Server._http._tcp,_ola, group 0x7f4698000ec0 changed to AVAHI_ENTRY>
Aug 14 10:15:45 tobias-pc olad[460]: olad/AvahiDiscoveryAgent.cpp:334: Adding _ola._sub._http._tcp
Aug 14 10:15:45 tobias-pc olad[460]: olad/AvahiDiscoveryAgent.cpp:236: State for OLA Server._http._tcp,_ola, group 0x7f4698000ec0 changed to AVAHI_ENTRY>
Aug 14 10:15:45 tobias-pc systemd[403]: Reached target Main User Target.
Aug 14 10:15:45 tobias-pc olad[460]: olad/AvahiDiscoveryAgent.cpp:236: State for OLA Server._http._tcp,_ola, group 0x7f4698000ec0 changed to AVAHI_ENTRY>
Aug 14 10:15:47 tobias-pc NetworkManager[371]: <info>  [1660464947.6585] manager: NetworkManager state is now CONNECTING
Aug 14 10:15:47 tobias-pc dbus-daemon[304]: [system] Activating via systemd: service name='org.freedesktop.resolve1' unit='dbus-org.freedesktop.resolve1>
Aug 14 10:15:47 tobias-pc NetworkManager[371]: <info>  [1660464947.7689] manager: NetworkManager state is now CONNECTED_SITE
Aug 14 10:15:49 tobias-pc systemd[1]: Reached target User and Group Name Lookups.
Aug 14 10:15:50 tobias-pc NetworkManager[371]: <info>  [1660464950.0039] manager: NetworkManager state is now CONNECTED_GLOBAL
Aug 14 10:15:50 tobias-pc systemd[658]: Queued start job for default target Main User Target.
Aug 14 10:15:50 tobias-pc systemd[658]: Reached target Paths.
Aug 14 10:15:50 tobias-pc systemd[658]: Reached target Timers.

[...]

Can someone help me how I can start this user service after the network is really online?


Why I want this:
I use ola and want to start olad at boot.
olad doesn't start properly (it doesn't activate plugins that require network) if there is no network connection. (See this issue)
When I read the issue I think the lack of olad handling network connections dynamically can be solved by starting it after network connection is established. Handling them dynamically should be something olad should do in the future. That is what is suggested:

Well-written software should be able to handle dynamic configuration changes. [1]

[1] Running Services After the Network is up

Last edited by topasiss (2022-08-14 09:42:59)

Offline

#2 2022-08-14 10:26:53

jonno2002
Member
Registered: 2016-11-21
Posts: 684

Re: systemd user service: Unable to configure service after network-online

in your

~/.config/systemd/user/olad.service 

try adding "ExecStartPre = /bin/nm-online -q " like this:

......
#ExecStartPre = /bin/sleep 5
ExecStartPre = /bin/nm-online -q 
ExecStart=/usr/bin/olad --log-level 3
......

or if you need to wait for actual internet access before starting then i have a script i use:

#!/bin/bash
until wget -q --spider duckduckgo.com
do
  sleep 10
done

and use that instead of nm-online

Offline

#3 2022-08-14 19:47:00

topasiss
Member
Registered: 2022-08-08
Posts: 50

Re: systemd user service: Unable to configure service after network-online

I would prefer a more general way to do this. Use the methods that already exist rather than scripting it or use something ment for something else for my purpose.

From the man pages for NetworkManager i got

For services that require network configured, NetworkManager-wait-online.service is the default implementation provided by NetworkManager to delay the target. But it does nothing magical. With special requirements, it may be sensible to disable NetworkManager-wait-online.service and replace it with a similar service that better implements the requirement.

I think overriding the NetworkManager-wait-online.service is in my view a way to make NetworkManager wait for connections properly, while following the general rules more.
I am afraid one day a script running somewhere might break something else.

Meanwhile I found

When a device reaches activated state, depends on its configuration. For example, with a profile with both IPv4 and IPv6 addressing enabled, the device is possibly considered fully activated when either of the address families is ready. This can be controlled with the ipv4.may-fail and ipv6.may-fail settings, to indicate that the address family is required.

at NetworkManager-wait-online.service and gave setting

ipv4.may-fail

a try.

[tobiasb@tobias-pc ~]$ sudo cat /etc/NetworkManager/system-connections/Wired\ connection\ 1.nmconnection
[connection]
id=Wired connection 1
uuid=5288477c-5f06-3209-bc0c-330e171b144c
type=ethernet
autoconnect-priority=-999
interface-name=enp5s0
timestamp=1660501992
zone=home

[ethernet]

[ipv4]
address1=192.168.[IpRemoved]/24,192.168.[IpRemoved]
dns=192.168.[IpRemoved];
may-fail=false
method=manual

[ipv6]
addr-gen-mode=stable-privacy
method=auto

[proxy]

Without success.

I could also try to find out the cause for the early online target availability by delaying the NetworkManager-wait-online.service with some sleep time in the PreExec section for debugging purposes.

Offline

#4 2022-08-23 11:33:05

topasiss
Member
Registered: 2022-08-08
Posts: 50

Re: systemd user service: Unable to configure service after network-online

I find it even more interesting to test Network services with NetworkManager dispatcher to start and restart the service on network changes.

Perhaps I'll try this, too.

Offline

#5 2023-03-22 17:26:11

topasiss
Member
Registered: 2022-08-08
Posts: 50

Re: systemd user service: Unable to configure service after network-online

Coming back to this issue these days and after reviewing the answers again want to say that this

jonno2002 wrote:

in your
try adding "ExecStartPre = /bin/nm-online -q " like this:

......
#ExecStartPre = /bin/sleep 5
ExecStartPre = /bin/nm-online -q 
ExecStart=/usr/bin/olad --log-level 3
......

is at already better than the simple sleep time that I used until now. So thanks for that @jonno2002.

Further I think I found the answer to this question:

topasiss wrote:

Now the network online target is reached before the service is started, but the network connection is established long after the service has started and I cannot find out why.

I wanted to get logs from nm-online that is called from NetworkManager-wait-online.service by removing the -q (quiet) from the nm-online call. I found no logs from nm-online in journalctl.
I checked if NetworkManager-wait-online.service was called at all and found it was not:

sudo systemctl show NetworkManager-wait-online.service --property=ActiveEnterTimestamp
ActiveEnterTimestamp=

So I guess I have to make sure that service is called properly.
Adding

wants=network-online.target
after=network-online.target

to the (user) unit and linking the system target into the user systemd seems to be not enough. It only helps to get the target reached, but the service NetworkManager-wait-online.service seems to be not called in that turn.

Offline

Board footer

Powered by FluxBB