Waiting for networks

I was revising some things in my startup scripts. I have a sort of generalized startup script in all the boxes in my constellation, which is capable of doing 8 or 10 different things that various of the boxes need to do at startup.

For example, the various gateway boxes need to open up (auto)ssh connections to my house with reverse tunnels so I can reach them. On some boxes I want them to open a vncserver so I can get a graphical environment up. On some others they may need to mount some filesystems, with smb or nfs. On some of them I want them to figure out where their router is, in case I want to open up their router in a browser. On some I need them to establish the keychain.

I noted that some of the raspbian pis would fail sometimes because the startup would run before the network was fully up, and I had to “relearn” that raspbian has a setting in raspi-config to wait for the network. Hmmm. How do they do that?

I dug around and I learned that if you ask raspbian to wait for the network what it actually does is to use dhcpcd5 -q -w. The w parameter says “Wait for an address to be assigned before forking to the background.” Cool.

I only use dhcpcd5 on the raspbian boxes, on most of the other linux boxes I use dhclient, or the inbuilt client in NetworkManager or systemd-networkd. While dhclient doesn’t have a similar parameter that I can find, NetworkManager does have a command for this: nm-online. On a system which used systemd-networkd and did not have NetworkManager, this can also be done with the one-shot systemd-networkd-wait-online service, but I haven’t done that.