You are not logged in.

#1 2016-10-15 21:04:52

EgidioCaprino
Member
Registered: 2016-03-13
Posts: 102

Run Systemd server when network is up

Hi!

I have a Systemd service like this one:

[Unit]
Description=My service
Wants=network-online.target
After=network.target network-online.target

[Service]
Type=oneshot
ExecStart=/my/script.sh

[Install]
WantedBy=default.target

The service is enabled and when I restart the system I have this service ran before the network is up. I know that because in the executed script I'm doing ifconfig > /tmp/ifconfig.txt and I can see that no interface is connected.

Why is the After directive not working properly? Do you have any idea?

Thanks,

Egidio

Offline

#2 2016-10-15 22:13:30

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Run Systemd server when network is up

What are you using to make network-online.target work?

Offline

#3 2016-10-15 22:14:44

EgidioCaprino
Member
Registered: 2016-03-13
Posts: 102

Re: Run Systemd server when network is up

I'm using wicd and the service is enable and works properly.

Offline

#4 2016-10-15 22:48:26

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Run Systemd server when network is up

I cannot easily find anything about wicd supporting network-online.target. Am I missing something or did you misread my question?

If you haven't already, read https://www.freedesktop.org/wiki/Softwa … orkTarget/

Offline

#5 2016-10-15 22:59:16

EgidioCaprino
Member
Registered: 2016-03-13
Posts: 102

Re: Run Systemd server when network is up

I thought wicd supported network-online.target. If you say it doesn't, then that's obviously the reason why it's not working.

I'm actually working on an AUR package. What if I add the NetworkManager-wait-online.service and systemd-networkd-wait-online.service to the After section but those services don't exist of are disabled? Will the service run anyway? I'm just trying to think about the broadest scenario.

Thank you for your help smile

Offline

#6 2016-10-15 23:25:31

rsmarples
Member
Registered: 2009-05-12
Posts: 287

Re: Run Systemd server when network is up

dhcpcd also has a dhcpcd-online program if that floats your boat.

Offline

#7 2016-10-17 01:30:53

Leonid.I
Member
From: Aethyr
Registered: 2009-03-22
Posts: 999

Re: Run Systemd server when network is up

I'd say, don't mess with any *-online services because their goal is not clear to begin with. If you need to test internet connectivity, just ping some host. For instance, networkmanager (I think) tries to connect to something at fedoraproject.org...

What do you mean by "interface not connected"? No IP address, not authenticated with a wireless AP? If so, your network.target should not be reached. Or wicd just forks dhcp anr/or wpa_supplicant daemon and declares itself done, thus reaching network.target. However, the above daemons can take arbitrary time to do what you call "connect".


Arch Linux is more than just GNU/Linux -- it's an adventure
pkill -9 systemd

Offline

#8 2016-10-17 10:19:06

rsmarples
Member
Registered: 2009-05-12
Posts: 287

Re: Run Systemd server when network is up

Leonid.I wrote:

I'd say, don't mess with any *-online services because their goal is not clear to begin with. If you need to test internet connectivity, just ping some host. For instance, networkmanager (I think) tries to connect to something at fedoraproject.org...

What do you mean by "interface not connected"? No IP address, not authenticated with a wireless AP? If so, your network.target should not be reached. Or wicd just forks dhcp anr/or wpa_supplicant daemon and declares itself done, thus reaching network.target. However, the above daemons can take arbitrary time to do what you call "connect".

Well, for dhcpcd-online, it only says it's online when all of the wait protocols set in dhcpcd.conf have been met.
The default is when any one protocol has fully finished. It makes no claims to working beyond what the protocol defines, ie it will not attempt to contact a fixed host.

The sole reason for it's existence is oddly enough because of systemd.
systemd prefers a non forking model for daemons .... but the normal daemon default (at least for DHCP clients and similar) is to fork once "up".
But because it no longer forks, it needs some other method of saying it's "up".
The other benefit * is that is that it could take down services when the network comes down and restart then when it comes back up.

* This isn't really a benefit, it's just masking tape for a badly written daemon.

Offline

#9 2016-10-17 12:01:35

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,532
Website

Re: Run Systemd server when network is up

rsmarples wrote:

Well, for dhcpcd-online, it only says it's online when all of the wait protocols set in dhcpcd.conf have been met.

This is all well and good, but seems quite tangential to the OPs question.

Unless I really missed something, the OP intends to distribute something that should work on arch systems in general.  Relying on the network-online.target being up to indicate network connectivity on arch systems in general is simply not feasible.  One can debate until we are blue in the face which package or which developers should be to blame for this, but it doesn't change the facts: network-online.target does not meet the OPs needs.

Pinging an arbitrary domain would work as would checking stats under /sys/class/net


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2016-10-17 14:52:36

rsmarples
Member
Registered: 2009-05-12
Posts: 287

Re: Run Systemd server when network is up

This is true.

OK, one thing which may meet the OP's needs is dhcpcd-run-hooks(8) which is still distributed in Arch base system as part of dhcpcd(8) (at least I think so, I don't actually use Arch).
So he can do something like this

$ cat /etc/dhcpcd.exit-hook
#!/bin/sh

if $if_up; then
    echo "Interface $interface is considered up" > /tmp/ifstatus
elif $if_down;
    echo "Interface $interface is considered down" >/tmp/ifstatus's point
else
    echo "Interface $interface is neither up nor down" >/tmp/ifstatus
fi

Of course, this does require using dhcpcd instead of some other tool such as systemd-network, network-manager, connman, dhclient, udhcpc, pump.

EDIT: And that's really the problem isn't it. Any user could be using any of these tools and there is no standard way to query all of them.
This goes back to Trilby's point that -online services are not feasable and my point that they just mask badly written daemons. In your case it looks like you're executing a script. Maybe ping a host in the script to assume connectivity as Tribly suggested.

Last edited by rsmarples (2016-10-17 15:21:36)

Offline

#11 2016-10-17 16:09:35

damjan
Member
Registered: 2006-05-30
Posts: 452

Re: Run Systemd server when network is up

The proper way for a service is to have After=network-online.target

The proper thing for the sysadmin to do is to enable the applicable *-wait-online.service so that it actually delays the network-online.target until it's "online" (for whatever meaning the *-online.service thiks it is online.)

Offline

#12 2016-10-18 00:47:20

Leonid.I
Member
From: Aethyr
Registered: 2009-03-22
Posts: 999

Re: Run Systemd server when network is up

rsmarples wrote:
Leonid.I wrote:

I'd say, don't mess with any *-online services because their goal is not clear to begin with. If you need to test internet connectivity, just ping some host. For instance, networkmanager (I think) tries to connect to something at fedoraproject.org...

What do you mean by "interface not connected"? No IP address, not authenticated with a wireless AP? If so, your network.target should not be reached. Or wicd just forks dhcp anr/or wpa_supplicant daemon and declares itself done, thus reaching network.target. However, the above daemons can take arbitrary time to do what you call "connect".

Well, for dhcpcd-online, it only says it's online when all of the wait protocols set in dhcpcd.conf have been met.
The default is when any one protocol has fully finished. It makes no claims to working beyond what the protocol defines, ie it will not attempt to contact a fixed host.

The sole reason for it's existence is oddly enough because of systemd.
systemd prefers a non forking model for daemons .... but the normal daemon default (at least for DHCP clients and similar) is to fork once "up".
But because it no longer forks, it needs some other method of saying it's "up".
The other benefit * is that is that it could take down services when the network comes down and restart then when it comes back up.

* This isn't really a benefit, it's just masking tape for a badly written daemon.

In archlinux, all *.service files shipped with dhcpcd are Type=forking...

BTW, rsmarples, thx for your work on dhcpcd smile

Last edited by Leonid.I (2016-10-18 00:48:42)


Arch Linux is more than just GNU/Linux -- it's an adventure
pkill -9 systemd

Offline

Board footer

Powered by FluxBB