[SOLVED] how to wait on network 11 February 2021, 11:38:53 I am setting up armtix runit on a rpi4; since the board has no rtc I have installed ntp and that works OK. I have a run that looks likeCode: [Select]# cat /etc/runit/sv/ntpd/run#!/bin/shntpd -g -q #run once and quitexec ntpd -g -u ntp:ntp -n >/dev/null 2>&1and this works, but leaves messages about not being able to connect. I guess I need to wait on the network, but I'm not sure how to do that. Currently the network is setup OK using dhcpcd so I could usesv check dhcpcd >/dev/null || exit 1but that will fail if I switch to another network setup. Are there more general ways to wait on the network? Quote Selected Last Edit: 03 March 2021, 15:12:37 by nous
Re: how to wait on network Reply #1 – 11 February 2021, 13:27:26 I used this scriptCode: [Select]$ cat /bin/have-net#/bin/shfor d in /sys/class/net/*;do [ `basename $d` = lo ] && continue [ "`cat $d/operstate`" = "up" ] && echo "connected" && exit 0doneecho "disconnected"exit 1and /bin/have-net > /dev/null || exit 1 in the scripts that need to wait on the network Quote Selected Last Edit: 11 February 2021, 21:43:12 by SGOrava 3 Likes
Re: how to wait on network Reply #2 – 11 February 2021, 16:54:00 That's a pretty clever idea. runit and s6 could definitely use a script like that. Thanks! Quote Selected 1 Likes
Re: how to wait on network Reply #3 – 11 February 2021, 21:11:14 Quote from: Dudemanguy – on 11 February 2021, 16:54:00That's a pretty clever idea. runit and s6 could definitely use a script like that. Thanks!I cannot claim too much credit. I think the idea of using /sys/class/net/eth0/operstate came from stack overflow. I just added the loop and this script is probably not robust enough against devices without operstate etc etc.Also it's not obvious which devices actually connect to the outside; Quote Selected Last Edit: 11 February 2021, 21:32:19 by replabrobin
Re: how to wait on network Reply #4 – 12 February 2021, 10:13:13 Quote from: replabrobin – on 11 February 2021, 21:11:14Also it's not obvious which devices actually connect to the outside; I have personal script for similar purpose, this is how i find actual interface inside it:Code: [Select]DEFAULT_ROUTE=DEFAULT_ROUTE_IFACE=while [ -z $DEFAULT_ROUTE -o -z $DEFAULT_ROUTE_IFACE ]; do DEFAULT_ROUTE=$(ip route|grep default|awk '{print $3}') DEFAULT_ROUTE_IFACE=$(ip route|grep default|awk '{print $5}') sleep 1 done Quote Selected
Re: how to wait on network Reply #5 – 12 February 2021, 17:47:34 So in the context of the runit loop that would just beCode: [Select]#/bin/sh[ "`ip route|grep default|awk '{print ($3!="")+($5!="")}'`" = "2" ] && echo "connected" && exit 0echo "disconnected"exit 1 Quote Selected
Re: how to wait on network Reply #6 – 12 February 2021, 17:59:41 Depending on your actual need you can simplify regex. The part of script i've posted defines which interface to check. In my case its gateway address is obtained to periodically ping gateway later and check if connection is present (but sometimes it still fails). If you're only interested to check network on boot, just checking if default route is present could be enough Quote Selected
Re: how to wait on network Reply #7 – 14 February 2021, 13:21:24 In the end following phoenix_king_rus' suggestion I made the have-net script look like Code: [Select]#/bin/shif [ -x /usr/bin/ip -a -x /usr/bin/awk ]; then [ "`ip route|awk '/^default/{print ($3!="")+($5!="")}'`" = "2" ] && echo "connected" && exit 0else for d in /sys/class/net/*;do [ `basename $d` = lo ] && continue [ "`cat $d/operstate`" = "up" ] && echo "connected" && exit 0 donefiecho "disconnected"exit 1 Quote Selected 1 Likes
Re: how to wait on network Reply #8 – 01 March 2021, 16:44:47 If you don't mind, I can include it on our runit scripts that require network (instead of running separate bin) and give proper credits. I'm sure the ip route and awk is enough (although the shell for loop is actually neat) since Artix has iproute2 and awk in base anyway.Thanks! Quote Selected 1 Likes
Re: [SOLVED] how to wait on network Reply #9 – 10 March 2021, 14:33:02 Quote from: konimex – on 01 March 2021, 16:44:47If you don't mind, I can include it on our runit scripts that require network (instead of running separate bin) and give proper credits. I'm sure the ip route and awk is enough (although the shell for loop is actually neat) since Artix has iproute2 and awk in base anyway.Thanks!Thanks Konimex I saw the change in the ntp-runit run script, but after finding it was taking a long time to work I had to add back the initial Code: [Select]ntpd -g -q #run once and quit I'm not sure why the -g option in the exec line doesn't do the same thing, but I think if there's no real time clock on the board (eg for the rpi4 which I am messing with) then the exec line takes a very long time to slew the system time from Jan 1 1970 up to the modern era. I think this might workCode: [Select]#!/bin/sh[ "$(ip route | awk '/^default/{print ($3!="")+($5!="")}')" = "2" ] || exit 1[ ! -c /dev/rtc ] && ntpd -g -q #run one large slew and quit if there's no hardware clockexec ntpd -g -u ntp:ntp -n >/dev/null 2>&1 Quote Selected
Re: [SOLVED] how to wait on network Reply #10 – 10 March 2021, 16:39:36 Quote from: replabrobin – on 10 March 2021, 14:33:02 I'm not sure why the -g option in the exec line doesn't do the same thing, but I think if there's no real time clock on the board (eg for the rpi4 which I am messing with) then the exec line takes a very long time to slew the system time from Jan 1 1970 up to the modern era. I think this might workI solve this problem by explicitly adding ntpdate into script:Code: [Select]ntpdate ru.pool.ntp.org || exit 1exec ntpd -g -u ntp:ntp -n >/dev/null 2>&1Replace pool with your preferred one. Works for me on both rpi4 and odroid n2 Quote Selected
Re: [SOLVED] how to wait on network Reply #11 – 10 March 2021, 20:42:11 Quote from: phoenix_king_rus – on 10 March 2021, 16:39:36Code: [Select]ntpdate ru.pool.ntp.org || exit 1exec ntpd -g -u ntp:ntp -n >/dev/null 2>&1according to https://linux.die.net/man/8/ntpdate the ntpdate functionality is actually part of ntpd now. The ntpdate manual page is not present on my rpi4, but that man page says the -q flag of ntpd does the same job and presumably the preferred pools are already set in /etc/ntp.conf Quote Selected