Discussion:
Autostart command after network is available (usbip)
(too old to reply)
Christian Britz
2022-02-08 14:50:01 UTC
Permalink
I want to know how I can execute a command as root once after the
network is available.

I activated usbipd on my home server and want to attach the client
automatically at boot. Manually attaching it works already.

The command to be executed as root on the client is:
usbip attach -r hostname -b n-n.n

Is this to be done via systemd? I was able to create a service on the
server for the daemon, but failed to do so for the client.
I also tried the root crontab using the @reboot timer, this failed
probably because of the network is not available at execution time.

Please share your ideas. It is probably quite simple, I was very
surprised that I could not find anything usefull on the web.

Regards,
Christian
--
http://www.cb-fraggle.de
Dan Ritter
2022-02-08 15:20:01 UTC
Permalink
Post by Christian Britz
I want to know how I can execute a command as root once after the
network is available.
I activated usbipd on my home server and want to attach the client
automatically at boot. Manually attaching it works already.
usbip attach -r hostname -b n-n.n
Is this to be done via systemd? I was able to create a service on the
server for the daemon, but failed to do so for the client.
probably because of the network is not available at execution time.
The normal methods for something that

- should run once
- as root
- after boot
- with a dependency on the network

would be:

- an init.d/ script with a dependency on network
- a systemd unit with a dependency on network
- an /etc/network/interfaces.d/ script run every time a
particular interface is brought up (i.e. post-up).

-dsr-
Greg Wooledge
2022-02-08 15:50:01 UTC
Permalink
Post by Christian Britz
I want to know how I can execute a command as root once after the
network is available.
Is this to be done via systemd?
Yes, that's where you should be doing this.

There's one thing in particular that you need to know first, and that's
how Debian defines interfaces for the purposes of determining that
"the network is available". Assuming that you're using
/etc/network/interfaces (and not Network-Manager or some other thing),
the interfaces that are maked as "auto" must be up, in order for Debian
to declare that the "network is available".

So, make sure your ethernet interface is marked as "auto" and NOT as
"allow-hotplug".

Once that's squared away, the rest is pretty straightforward.

Create a new systemd service (/etc/systemd/system/yourname.service),
and in the [Unit] section, make sure you've got this:

After=network-online.target

That's basically it. Do the daemon-reload thing, then enable and start
the service, and if it works, reboot to really test it.
Christian Britz
2022-02-08 16:50:01 UTC
Permalink
Post by Greg Wooledge
Post by Christian Britz
I want to know how I can execute a command as root once after the
network is available.
Is this to be done via systemd?
Yes, that's where you should be doing this.
That is what I thought of initially. And I think I am _almost_ there.
The script works, after several hours of trial, errors and your and
Dan's advice, when I manually execute it via systemctl. Start and stop
works.

It still does not work during boot time, though:
Feb 08 17:34:53 amiga5000 usbip[1014]: usbip: error: tcp connect
Feb 08 17:34:53 amiga5000 systemd[1]: Starting usbip client service...
Feb 08 17:34:53 amiga5000 systemd[1]: usbip.service: Main process
exited, code=exited, status=1/FAILURE
Feb 08 17:34:53 amiga5000 systemd[1]: usbip.service: Failed with result
'exit-code'.
Feb 08 17:34:53 amiga5000 systemd[1]: Failed to start usbip client service.

I am almost sure I got the targets wrong. I _am_ actually using
Network-Manager. Here is my service file:

Unit]
Description=usbip client service
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/sbin/usbip attach -r raspberrypi -b 1-1.4
RemainAfterExit=true
ExecStop=/usr/sbin/usbip detach -p 00
StandardOutput=journal

[Install]
WantedBy=multiuser.target
Post by Greg Wooledge
There's one thing in particular that you need to know first, and that's
how Debian defines interfaces for the purposes of determining that
"the network is available". Assuming that you're using
/etc/network/interfaces (and not Network-Manager or some other thing)
Yes, I need some more help in the context of using Network-Manager.
--
http://www.cb-fraggle.de
Greg Wooledge
2022-02-08 16:50:02 UTC
Permalink
Post by Christian Britz
Post by Greg Wooledge
There's one thing in particular that you need to know first, and that's
how Debian defines interfaces for the purposes of determining that
"the network is available". Assuming that you're using
/etc/network/interfaces (and not Network-Manager or some other thing)
Yes, I need some more help in the context of using Network-Manager.
"Don't."

Unless this is a laptop or something, in which case, I'm just throwing
my hands up and stepping away.
Christian Britz
2022-02-08 17:00:01 UTC
Permalink
Post by Greg Wooledge
Post by Christian Britz
Yes, I need some more help in the context of using Network-Manager.
"Don't."
Unless this is a laptop or something, in which case, I'm just throwing
my hands up and stepping away.
Yes, the client is a laptop. Network-Manager seems to be the default
solution choosen by the Debian installer. I don't have problems with
changing interface names or something, are there more reasons to avoid
it on a desktop (KDE Plasma in my case)?

Before you step away, please take notice that you helped me a lot. ;-)
--
http://www.cb-fraggle.de
Christian Britz
2022-02-08 16:50:02 UTC
Permalink
Post by Christian Britz
[Install]
WantedBy=multiuser.target
Changed that to

[Install]
WantedBy=network-online.target

Now it works! :-)
--
http://www.cb-fraggle.de
Andrei POPESCU
2022-02-09 14:50:03 UTC
Permalink
Post by Christian Britz
Post by Christian Britz
[Install]
WantedBy=multiuser.target
Changed that to
[Install]
WantedBy=network-online.target
That's like putting the carriage before the horse ;)

(and it probably works only by chance)

You need Wants=network-online.target in addition to After=, see
'man systemd.special' for details.

Kind regards,
Andrei
--
http://wiki.debian.org/FAQsFromDebianUser
Andy Smith
2022-02-09 06:10:01 UTC
Permalink
Hello,
Post by Christian Britz
Post by Greg Wooledge
Post by Christian Britz
Is this to be done via systemd?
Yes, that's where you should be doing this.
That is what I thought of initially. And I think I am _almost_ there.
The script works, after several hours of trial, errors and your and
Dan's advice, when I manually execute it via systemctl. Start and stop
works.
Last time I was trying to make something start only after the
network was up I found I needed more than
Wants/After=network-online.target when using ifupdown. I wrote it up
here:

https://strugglers.net/~andy/blog/2020/12/03/starting-services-only-when-the-network-is-ready-on-debiansystemd/

Cheers,
Andy
--
https://bitfolk.com/ -- No-nonsense VPS hosting
황병희
2022-02-09 12:00:01 UTC
Permalink
[... long line snip ...]
https://strugglers.net/~andy/blog/2020/12/03/starting-services-only-when-the-network-is-ready-on-debiansystemd/
Thanks Andy! Added into my bookmark Firefox.

Sincerely, Linux fan Byung-Hee
Loading...