Waiting for network link to be up before continuing in bash
18,675
You can check if the network interfaces' names appear after running ifconfig -s
.
Something like:
if [ $( ifconfig -s | grep eth0 ) ]; then echo "eth0 is up!"
Check this link for more details.
To make this test ongoing, you can do something like what @jm666 said:
while ! ping -c 1 -W 1 1.2.3.4; do
echo "Waiting for 1.2.3.4 - network interface might be down..."
sleep 1
done
Author by
user1527227
Updated on July 24, 2022Comments
-
user1527227 almost 2 years
Is there a way to check for successful network interface link for multiple interfaces in a bash script before continuing?
Something like:
eth0 eth1 eth2 eth3 network interfaces are brought up Wait for link detection on all 4 interfaces Proceed
-
user1527227 almost 10 yearsThis is great if I just want to test once, but what if I need to actively wait for until it comes online and then proceed?
-
user1527227 almost 10 yearsWhy do you have a
-W
flag in there? You didn't specify a timeout for a single ping.... -
jimm-cl almost 10 years
-c 1 -W 1
means "try 1 echo, and wait for it for 1 second at most". If it does not receive the echo back, then enters the if-block, shows the message, and wait for one second before trying again. -
user1527227 almost 10 yearsIf you didn't specify
-W 1
... how long would it wait for? I dont see a descrp in the man pages -
jimm-cl almost 10 yearsAs far as I understand,
-c 1
alone will just send one echo and wait for it to return, no matter how long it takes. Adding the-W 1
will prevent this from waiting too long, allowing just 1 second of time to get a response. -
logidelic over 4 yearsThe while loop as presented (
while ! ping -c 1 -W 1 1.2.3.4;
) doesn't work for me on Ubuntu 18... It will correctly continue if the network is not available but will also continue if I get a valid ping result. I had to change it towhile ! (ping -c 1 -W 1 1.2.3.4 | grep -q 'statistics');